SpoilerAlert /Help/ Refactoring Global Var out of Functions

Good Morning all !

Seems I am still unsure why things went wonky here.

Here is the code :

// the global variable
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];

/* This function should add a book to the list and return the list */
// New parameters should come before the bookName one

// Add your code below this line

let myBooks=bookList.slice();


function add (myBooks,bookName) {
  
  return myBooks.push(bookName);
  
  // Add your code above this line
}

/* This function should remove a book from the list and return the list */
// New parameters should come before the bookName one

// Add your code below this line
function remove (myBooks,bookName) { 
  if (myBooks.indexOf(bookName) >= 0) {
    let myIndex=myBooks.indexOf(bookName);
    return myBooks.splice(myIndex,1);
   
    
    // Add your code above this line
    }
}

var newBookList = add(myBooks, 'A Brief History of Time');

var newerBookList = remove(myBooks, 'On The Electrodynamics of Moving Bodies');

var newestBookList = remove(add(myBooks, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');

console.log(bookList);

Now it is complaining about the myBooks.indexOf not being a function. also the console.log (bookList) is not spewing forth the original list. In testing I have moved a console.log(myBooks) around a bit and that all seems fine. until you get to the bottom.

Finally , in checking some of the other solutions available, I noted that for some reason everyone seems to be putting the copy of the array into the function instead of in the global and using that as I have. Why is that ?

Thank you in advance. I am sure its just early morning non-caffinated brain, but thank you for your time. in advance. it is greatly appreciated !

Alright, I am game… let me make a small clarification on my own assumption. I was actually assuming that the PUSH function would just insert the appropriate item to the end of the myBooks array , and that there would be the inference of the entire myBooks array that would be fed to the REMOVE portion of the function. Interestingly this function does work in Codepen, but not here. since it does not , as you correctly say, remove the global variable.

As an abstract, maybe you could clarify for me why one would want to do that if ( and you can correct my thought since if you ask my wife I can actually be wrong LOL ) you use the global variable instead of having that inside the array that the functionality of the program would be more efficient.
I say that with the understanding that the point is that having the variable outside of the function defeats the modularity (?) of the function its self ?

I will take a stab at re-writing this as it was given there are 2 options I looked at the SLICE and the […ARRAY} options that would accomplish that.

Thank you again for taking the time to teach. I greatly appreciate the gift.

Cannot solve this one, does this lesson have a bug?

When i try the code on directly on browser using a code snippet ( google chrome ) - it is ok.