Functional Programming: Refactor Global Variables Out of Functions Last task not passing

Tell us what’s happening:
I got the first three portions of this challenge to pass. The last one is quite confusing as it is a function within a function. Am I adding a new Book into the array and then subtracting one immediately at index of functional declaration of newstBookList???
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"];

// Change code below this line
function add (array, bookName) {
return array.concat(bookName);

// Change code above this line
}

// Change code below this line
function remove (firstArray, bookName) {
var book_index = bookList.indexOf(bookName);
if (book_index >= 0) {

  firstArray.splice(book_index, 1);
  return  firstArray; 

  // Change code above this line
  }
}

var newBookList = add(bookList, 'A Brief History of Time');
var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
//first we're adding a function within a function, which appears to be adding one book, and removing a book?
var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');

// console.log(bookList);
console.log(newerBookList)
// console.log(newestBookList); 

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36.

Challenge: Refactor Global Variables Out of Functions

Link to the challenge:

your issue is you are not doing this:

Rewrite the code so the global array bookList is not changed inside either function.

splice change the array on which is used, in the case of below, the array passed in as first parameter is changed

1 Like

I see. Hmmm I’ve tried slice but then I can’t seem to take the index of the array out that I want for the 3rd portion of the challenge

Ah nevermind. Had to create a copy of the array. Thank you!!! got it figured out =)