Refactor Global Variables Out of Functions - indexOf() is not a function?

Tell us what’s happening:

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 (bookList, bookName) {
  
  
  return bookList.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 (bookList, bookName) {
  let index = bookList.indexOf(bookName);
  if (index >= 0) {
    
    return bookList.splice(index, 1);
    
    // 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

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

Why does it keep saying bookList.indexOf is not a function?

  1. You are altering the global variable booklist and you shouldn’t do that.
  2. Your functions add and remove requires an array along with a string as parameters. The current code is changing the original booklist. It shouldn’t. Change that reference parameter from booklist to arr (you may give any name of your choice) .
  3. Don’t forget to change the parameter in the return statements.
  4. Re-check the logic of your code.
1 Like

You get that error for this line. What does push return?

1 Like

Thank you guys! I have figured it out and passed it.

1 Like