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

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/

Nevermind. I solved it.

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

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?

Not sure if this is helpful, however I came up with a solution that i think is easy to understand and gets the job done.

// 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 blist = [...bookList]; //copy contents of array
  blist.push(bookName); // push in new contents into array
  return blist;//return the new array 
  
  // 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 blist = [...bookList]; //copy contents of array
  if (blist.indexOf(bookName) >= 0) {
    let index = blist.indexOf(bookName);
    blist.splice(index, 1);  
    return blist;
    // 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);
1 Like