Tell us what’s happening:
Describe your issue in detail here.
So I solve this problem correctly but I feel like the hint in the prompt misled me. It says:
Hint: using something like const newArr = arrVar
, where arrVar
is an array will simply create a reference to the existing variable and not a copy. So changing a value in newArr
would change the value in arrVar
.
When I tried something like this I always failed the first test. But it works fine once I figured out that I need to use the spread operator to copy the list over instead of only the ‘=’
**Your code so far**
// The global variable
const bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
// Change code below this line
function add(list, bookName) {
let newBookList = [...list];
newBookList.push(bookName);
return newBookList;
// Change code above this line
}
// Change code below this line
function remove(list,bookName) {
const book_index = bookList.indexOf(bookName);
const newBookList = [...list];
if (book_index >= 0) {
newBookList.splice(book_index, 1);
return newBookList;
// Change code above this line
}
}
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
Challenge: Refactor Global Variables Out of Functions
Link to the challenge: