Tell us what’s happening:
first post here and after a thousand doubts behind,
i’ve found this Solution 2 so elegant and concise
but i have few questions
in add,
[…list,bookName]
is using spread operator to get all the books in the bookList array?
is it coyping them?can we return it without side effect?
in remove,
filter is going to be executed only if there’s an index for book?
why we can’t use spread inside if?
item !== is for not a bookname in the array?
and again how can we return safe ?
thanks
Your code so far
function add (list,bookName) {
return [...list,bookName]
}
function remove (list,bookName) {
if (list.indexOf(bookName) >= 0) {
return list.filter((item)=> item !== bookName);
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/refactor-global-variables-out-of-functions