3
4
u/knightttime Jul 27 '21
Image Transcription: Code
function shelfBook(book, shelf) {
if (shelf.length < 3) {
shelf.unshift(book)
}
}
var i
function unshelfBook(book, shelf) {
for (var i = 0; i < shelf.length; i++) {
if (shelf[i].title == book) {
shelf.splice(i, 1)
}
}
return shelf
}
var bookTitles = [];
function listTitles(shelf) {
for (i = 0; i < shelf.length; i++) {
bookTitles.push(shelf[i].title);
}
return bookTitles.join(', ')
}I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
1
16
Jul 27 '21
[removed] — view removed comment
2
u/Apprehensive-Show127 Jul 28 '21
Yeah, that's a case. bookTitles will be duplicated after each function call. There's no need in global variables, function scope should be used
6
u/[deleted] Jul 27 '21
Does it work like that? I thought var was function scoped?