Refactor Global Variables Out of Functions - Error in the copies?

Tell us what’s happening:

I don’t get what is wrong, i make a copy of the array into another variable but still didn’t pass the test

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) {
var newBookList = bookList.slice();
newBookList.push(bookName);
return newBookList;

// Change code above this line
}

// Change code below this line
function remove (bookName) {
var book_index = bookList.indexOf(bookName);
if (book_index >= 0) {
  var newBookSlice = bookList.slice();
  newBookSplice.slice(book_index, 1);
  return newBookSlice;

  // 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 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0.

Challenge: Refactor Global Variables Out of Functions

Link to the challenge:

newBookSplice.slice(book_index, 1); i think there is a typo here

your code is not reusable tho

and you are missing this:

  1. Declare function parameters - any computation inside a function depends only on the arguments passed to the function, and not on any global object or variable.

i should send the array of books as an argument to the functions?

still don’t pass the challenge after fixing the typo .


// Change code below this line
function add (bookList,bookName) {
  let newBookList = [...bookList];
  newBookList.push(bookName);
  return newBookList;
  
  // Change code above this line
}

// Change code below this line
function remove (bookList,bookName) {
  var book_index = bookList.indexOf(bookName);
  if (book_index >= 0) {
    let newBook = [...bookList];
    newBook.splice(newBook.indexOf(book_index), 1);
    return newBook;

    // Change code above this line
    }
}

what’s book_index?

1 Like

got it, i see the problem now
Thank you.