Refactor Global Variables Out of Functions Final Test

Tell us what’s happening:

Hey everyone :slight_smile: I’m struggling to wrap my head around what’s happening in the final test case here. Could anyone please help me with an explanation? Thanks in advance :slight_smile:

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 (added,bookName) {
  
  added = bookList.concat(bookName);

  return added;
  
  // 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 (removed,bookName) {
  removed = [...bookList];
  while (bookList.indexOf(bookName) >= 0) {
    
    removed = bookList.filter(a => a !== bookName);
    
    return removed;
    // 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/

watch out for your add function. It should return an array… (but it isn’t doing that).

Now about the testcases:
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’);

As you know, newBookList is the new list of books with “A Brief History of Time” added to the list.

newerBookList is the same as bookList but with one item removed “On The Electrodynamics of Moving Bodies”

newestBookList is the same as bookList but with one item added and one item removed. (add A Brief History of Time" and remove “On The Electrodynamics of Moving Bodies”)