Refactor Global Variables Out of Functions hellp

Tell us what’s happening:
I am trying to understand this but I can’t figure out what I’m doing wrong. The remove function doesn’t appear to be working

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 (bookName) {
  let arr=bookList.slice(0);
  arr.push(bookName);
  return arr;
  
  
  // 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 (bookName) {
  if (bookList.indexOf(bookName) >= 0) {
    let copy = bookList.slice(0);
    if(copy.indexOf(bookName)>=0 ){
      copy.splice(copy.indexOf(bookName), 1);
    }
    return copy;
    
    // 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

The key is the above comment in the code. Remove needs an extra parameter before bookName, because if you look at the calls to the function named remove, you will see it is passing in the bookList array as the first argument and name of the book as a second argument. Since your remove function only takes one argument, inside your function bookName is the bookList array. Technically, your add function should have two parameters also.

1 Like