I am trying to use clones of bookList so that I these new arrays can return added or deleted elements without modifying the original array. Can I please get a hint on what could have gone wrong? Thank you.
**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 (bookName) {
let booklistadd = [...bookList]
booklistadd.push(bookName);
return booklistadd;
// Change code above this line
}
// Change code below this line
function remove (bookName) {
let bookRemoved = [...bookList]
const book_index = bookRemoved.indexOf(bookName);
if (book_index >= 0) {
bookRemoved.splice(book_index, 1);
return bookRemoved;
// Change code above this line
}
}
const newBookList = add(bookList, 'A Brief History of Time');
const newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
const 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; rv:95.0) Gecko/20100101 Firefox/95.0
Challenge: Refactor Global Variables Out of Functions
I tried that as well and I still don’t seem to get it:
// 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 (bookArr, bookName) {
bookArr = [...bookList]
bookArr.push(bookName);
return bookList;
// Change code above this line
}
// Change code below this line
function remove (bookArr, bookName) {
bookArr = [...bookList]
const book_index = bookArr.indexOf(bookName);
if (book_index >= 0) {
bookArr.splice(book_index, 1);
return bookArr;
// Change code above this line
}
}
const newBookList = add(bookList, 'A Brief History of Time');
const newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
const newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');
console.log(bookList);
yes and I am still not getting it, do you know any resource I could reference to in order to understand this better ? If you do, I would really appreciate it! Thank you…
Inside of these functions, you are only allowed to use the variables bookArr and bookName.
So lets look at the function calls to see what these are:
The bookList that you want to modify is passed in as an argument, so you shouldn’t reference a specific bookList inside of your function.
You are close here. It’s just a matter of only using the variables declared as function arguments and never directly using the global variables themselves.
Yes, I get it know, I just needed to enter a landing arr (as parameter) to both add() and remvoe() so that these could hold the elements of bookList and be transformed to a newArr when bookList is entered as a parameter…
Yes it does, but I think I over complicated it, (as you can read from the explanation of my own solution) just like pretty much everything I tend to do on Js, but hey, you’ve been a great guide and I really appreciate your disposition (and patience… with me)