Refactor Global Variables SLICE/SPREAD - SPOILERT

I completed the challenge using spread operator, but when I tried using slice() it did not copy the arrays as well into the new variable, can anyone tell me why and when I should use spread operator instead of slice and vice versa

  **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 (list, bookName) {
let news = [...list]
news.push(bookName);
return news;

// Change code above this line
}

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

  // 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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67

Challenge: Refactor Global Variables Out of Functions

Link to the challenge:

can you show an example of code not working?

for example, I did this:

let news = list.slice()
news.push(bookName);
return news;

it should work the same, there is no reason for which why that shouldn’t work

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