Refactor Global Variables Out of Functions indexOf is not a function

Refactor Global Variables Out of Functions indexOf is not a function
0

#1

Tell us what’s happening:

Can not understand why “bookList.indexOf is not a function”, as I see, an array is always passed as an argument

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

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


#2

@bpalij,

I don’t know why you are getting an error from using indexOf, as indeed bookList is an array, but from reading the instructions for this challenge, you must do the following:

Refactor (rewrite) the code so the global array bookList is not changed inside either function. The add function should add the given bookName to the end of an array. The remove function should remove the given bookName from an array. Both functions should return an array, and any new parameters should be added before the bookName one.

Try writing in the missing code and seeing if it passes the tests.

Good luck


#3

The problem is the above line returns a number instead of an array. Why? Because push returns the length of an array after a new element has been added.

Also, in your remove function (seen below), your splice always removes the first element of the array (the first parameter value), instead of the element equal to the bookName.

function remove (bookList, bookName) {
  if (bookList.indexOf(bookName) >= 0) {
    return bookList.slice(0,).splice(0, 1, bookName);
  }
}

#4

Thank you, that was not obvious. About remove - understood, that something is wrong, but did not want to fix beacause of “indexOf” error.


#5

Indeed, that “bookList.indexOf is not a function” should not be there :grinning: