Refactor Global Variables Out of Functions Help

Refactor Global Variables Out of Functions Help
0.0 0

#1

Tell us what’s happening:
Hi, can anyone help me with my code. Thank You!

I cannot seems to figure out what wrong.

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) {
  
  var copyBookList=bookList.slice();

  copyBookList.push(bookName);

  return copyBookList;
  
  // 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) {

  var copyBookList=bookList.slice();
let n=copyBookList.indexOf(bookName);
if(n>=0){
  copyBookList.splice(n,1)
  return copyBookList;

    
    // 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/66.0.3359.181 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

Try adding in some console.log statements before the return statements of your functions, to confirm what values you are returning.

Your mistake is you call your add and remove functions with two arguments, but then only have one parameter in those function’s declarations.

Also, you should not reference any global variables inside your add or remove functions. Currently, you are using slice on bookList, which is a global variable. Pass it into the function to avoid this.