Computer doesnt accept my correct answer :)

Tell us what’s happening:
Hi,
i have tried only newBookList seems i fail and can not figure out the reason why.
can someone please help me? newBookList returns the correct value however, computer doesnt accept it

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 bookName

// Add your code below this line
function add (bookList1,bookName) {
var a= bookList1;
a.push(bookName);
return a;

// 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 book_index = bookList.indexOf(bookName);
if (book_index >= 0) {

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

  // Add your code above this line
  }
}

var newBookList = add(bookList, 'A Brief History of Time');
console.log(newBookList)
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');


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36.

Challenge: Refactor Global Variables Out of Functions

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

Your remove function is still referencing the global variable.

the problem which i am trying to address now is why add function doesnt work properly

this is not how you copy an array, any change to a will be reflected to bookList1

I suggest you do a small research on how to copy an array

thank u. for those who dont know:
i have used the code below:
function add (bookList1, bookName) {
var a = […bookList1]
a.push(bookName);
return a;

// Add your code above this line
}

1 Like

look for detailed information about arrays