Refactor Global Variables Out of Functions Why this doesn't work

Refactor Global Variables Out of Functions Why this doesn't work
0.0 0

#1

Tell us what’s happening:

It passes the first three tests but the last one doesn’t succeed, why?
In console, newestBookList returns undefined.

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 List = bookList.slice(0);
  List.push(bookName);
  return List;
  // 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 List = bookList.slice(0);
  if (List.indexOf(bookName) >= 0) {
    List.splice(List.indexOf(bookName),List.indexOf(bookName));
    return List;
    // Add your code above this line
    }
}

var newBookList = add('A Brief History of Time');
var newerBookList = remove('On The Electrodynamics of Moving Bodies');
var newestBookList = remove(add('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; rv:60.0) Gecko/20100101 Firefox/60.0.

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


#2

Nevermind. I solved it.

It seems that everything was right except adding arguments to know which array is. That’s all.


#3

Can you please post your solution? Also, I have noticed that your original code is changed where it should not be. Difference being this:

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’);

Can you explain please?