Refactor Global Variables Out of Functions(Booklist.slice()is not a function?)

Refactor Global Variables Out of Functions(Booklist.slice()is not a function?)
0

#1

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) {
  let copy = bookList.slice(0);
  return copy.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 copy = bookList.slice(0)
  if (copy.indexOf(bookName) >= 0) {
    
    return copy.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 (Macintosh; Intel Mac OS X 10_11_6) 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

The documentation for splice says that it returns an array of deleted elements

So your code for ‘remove’ is returning an array of deleted elements instead of the updated array
try to do the splice first then return copy on a different line.


#3

Also note that if you return whatever push returns you are actually returning the length of the new array…


#4

The actual error I got is “booklist.slice() is not a function”, which makes me confused.


#5

the reason bookList.slice() is not a function is because you have made bookList into a number…(instead of an array)
(because you return the number of books instead of the array)

if you fix your add and remove function, and always return an array, the error will go away…