Was I wrong to use spread operator here?

So I wrote:

// 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) {
  let newList = [...bookList]; 
  newList.push(bookName);
  return newList;
  
  // Change code above this line
}

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

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

    // 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);

You need to add bookList as your first argument.

They are calling the functions with bookList as the first argument so what you think is bookName is actually bookList

So spread operator was right I just had to change parameters?

Yes, your code works fine given the parameters are in the right order.

They do make a note of it, but I think their choice as to what they start your code off with makes it unnecessarily confusing.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.