Tell us what’s happening:
Can someone explain to me why my Add function works? Shouldn’t it be wrong? So, because the concat method creates a new array, does it work?
Sorry for the inconvenience, I appreciate your help.
Your code so far
// the global variable
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
/* This function should add a book to the list and return the list */
// New parameters should come before bookName
// Add your code below this line
function add (list, bookName) {
let addBook = list.concat(bookName);
//var value = copyList.push(bookName);
return addBook;
// Add your code above this line
}
/* This function should remove a book from the list and return the list */
// New parameters should come before the bookName one
// Add your code below this line
function remove (list, bookName) {
var book_index = list.indexOf(bookName);
if (book_index >= 0) {
let copyList = [...list];
copyList.splice(book_index, 1);
//list.splice(book_index, 1);
return copyList;
// Add your code above this line
}
}
var newBookList = add(bookList, 'A Brief History of Time');
var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');
console.log(bookList);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36
.
Challenge: Refactor Global Variables Out of Functions
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/refactor-global-variables-out-of-functions