Refactor Global Variables Out of Functions (appearently it's not a function)

Tell us what’s happening:
The error message I keep getting is “bList.indexOf is not a function”. If I replace bList with booklist (which I shouldn’t do but it wouldn’t change the array, so whatever) then I get “bList.splice is not a function”. Somehow, the function runs perfectly fine with the global variable but it won’t run if I substitute my own. What gives?

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"];

/* 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
function add (bList, bookName) {
  
  return bList.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 (bList, bookName) {
  if (bList.indexOf(bookName) >= 0) {
    
    return bList.splice(0, 1, bookName);
    
    // Add your code above this line
    }
}

var newBookList = add(bookList, 'A Brief History of Time');
var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
var 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 (X11; Linux x86_64; rv:52.9) Gecko/20100101 Goanna/4.1 Firefox/52.9 PaleMoon/28.0.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/refactor-global-variables-out-of-functions/

First thing I would suggest, make a copy of bList rather than editing that one. You’re working on the global variable at that point, as its being passed by reference. Use the spread operator and clone that, giving you a clean copy each time. Doing that made the bList error go away, and also made add() do as it should.

Second, the parameters on your splice() are wrong. You don’t want to splice at the start of the array, you want to splice starting at that indexOf value.

Works now after some additional changes, thanks.