I understand that the code shouldn’t effect the original bookList, so I created a new variable, but beyond that I’m not sure how to solve this. I thought maybe I could google alternatives to push and splice? Thanks
**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"];
var freshBookList = bookList;
// Change code below this line
function add (bookName) {
freshBookList.push(bookName);
return freshBookList;
// Change code above this line
}
// Change code below this line
function remove (bookName) {
var book_index = freshBookList.indexOf(bookName);
if (book_index >= 0) {
freshBookList.splice(book_index, 1);
return freshBookList;
// Change code above this line
}
}
var newBookList = add(freshBookList, 'A Brief History of Time');
var newerBookList = remove(freshBookList, 'On The Electrodynamics of Moving Bodies');
var newestBookList = remove(add(freshBookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');
console.log(newBookList);
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36.
Challenge: Refactor Global Variables Out of Functions
When you do that, freshBookList will still reference the original array bookList. You need to create a copy of the array. You can read more in the article below.
@ILM ah I see, so I was just declaring it as global and fixed it by declaring in the variables. I ended up finding that I had to define a unique property (if that’s the right term) within the function ie function add (originalBookList, bookName) and function remove (originalBookList,bookName).
I don’t remember learning that I could enter any property name I wanted like originalBookList inside the function. Do you know which exercise covered this? These functions are still new to me, so I haven’t gotten it down yet.
Declare function parameters - any computation inside a function depends only on the arguments passed to the function, and not on any global object or variable.
and
Note: Both functions should return an array, and any new parameters should be added before the bookName parameter.