Need more explaination how to solve this

Tell us what’s happening:

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"];

// Change code below this line
function add (bookName) {

bookList.push(bookName);
return bookList;

// Change code above this line
}

// Change code below this line
function remove (bookName) {
var book_index = bookList.indexOf(bookName);
if (book_index >= 0) {

  bookList.splice(book_index, 1);
  return bookList;

  // Change 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 (X11; CrOS x86_64 13099.85.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.110 Safari/537.36.

Challenge: Refactor Global Variables Out of Functions

Link to the challenge:

In the criteria below you see that bookList is an argument in both add and remove functions. This is not so in your code.
You are changing the array bookList. You shouldn’t do that. You need to create a new array that is a copy of bookList and then you need to add or remove items from that new array. So make a copy of bookList first in your add and remove functions and work with that (you can use the spread operator newarray = [… array]

1 Like