Refactor Global Variables Out of Functions arr.indexOf is not a function

Tell us what’s happening:
what is the wrong with my code?
arr.indexOf is not a function

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 (arr, bookName) {
  
  return arr.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 (arr, bookName) {
  if (arr.indexOf(bookName) >= 0) {
    let newList = arr.slice(0, arr.indexOf(bookName)).concat(arr.slice(arr.indexOf(bookName) + 1));
      return newList;
    // 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/71.0.3578.98 Safari/537.36.

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

Here are at least two things.

  1. Try logging out the type of newBookList
var newBookList = add(bookList, 'A Brief History of Time');
console.log(typeof newBookList);
  1. bookList should not change and still equal the same array (look into reference type), do not mutate the bookList array, make a copy before changing it and return the copy.
1 Like