I believe there is an issue with Refactor Global Variables Out of Functions.
I am expecting a single value for the parameter of the add function bookName but instead I am getting the entire bookList passed in. It looks like from the solution that this used to be the case but somewhere along the line this must have been refactored and it is now broken.
// 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) {
return [...bookList, bookName];
// Change code above this line
}
// Change code below this line
function remove(bookName) {
return [...bookList].filter(book => book !== bookName);
// Change code above this line
}
User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36
Challenge: Refactor Global Variables Out of Functions
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 the challenge instructions
Note: Both functions should return an array, and any new parameters should be added before the bookName parameter.
You are supposed to add any parameters to avoid using global variables.
Referring to Solution #1: I still don´t understanding something basic
How does the code know that we are referring with the first and new parameter (which has to be added according to the incstructions) to the bookList Array? This keeps puzzling me…
Look at where the function we define are being called. You will notice as arguments are provided the bookList and the bookName. Ofc, the way functions work, you could provide any other data with similar structure and they would work the same. That is why it is nice to have any global variables out of functions, so functions are universal and do not affect globals directly
Nothing is added to or removed from the bookList array. Both the add and remove functions return a new version of the bookList array.
The way the solution code shows the first 2 function calls is a bit misleading as the return is not used for the next function call.
If you call add using bookList and then call remove using bookList you now have 3 versions of the book list. If you include the last function call you have 4 versions.
The original bookList
The one where a book was added newBookList
The one where a book was removed newerBookList
The one where a book was added and removed newestBookList
The lists are completely out of sync which doesn’t really make much sense. If you add or remove a book you have to use the new book list returned as the new state.
Now I got it! It totally didn´t pay any attention to the function call … Many thanks for your effort of patience @JeremyLT@Sylvant and lasjorg (sorry for not tagging but I can´t tag more than two people)
Maybe trying to learn JS at 5 in the morning is not always the best idea