Hi all, I know I can look at the other posts with the same question, but most likely their code is different, and I want to know why my code is not working.
link to the question:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/refactor-global-variables-out-of-functions
So everything passes except the output for newBookList.
Which is puzzling, because when I add a document.write(newBookList) underneath the line : var newBookList = add(tempBookList, ‘A Brief History of Time’);
it shows the correct output ?
Here’s my code:
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
let tempBookList = bookList.slice();
function add (list,bookName) {
list.push(bookName);
return list;
}
function remove (list,bookName) {
var book_index = list.indexOf(bookName);
if (book_index >= 0) {
let begin=list.slice(0,book_index);
let end = list.slice(book_index+1,list.length-1);
let newlist= begin.concat(end);
return newlist;
} else {
return list;
}
}
var newBookList = add(tempBookList, 'A Brief History of Time');
document.write("<br/><br/>"+ newBookList); //shows the correct output?
var newerBookList = remove(tempBookList, 'On The Electrodynamics of Moving Bodies');
const added = add(tempBookList,'A Brief History of Time');
var newestBookList = remove(added, 'On The Electrodynamics of Moving Bodies');
Thanks for any help !