Refactor Global Variables Out of Functions - Am I missing something?

Hi there. It seems the bookList array is changing. I looked up how to clone an array and tried using the Spread Operator, for loop and Array.from.

// The global variable
const bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];

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

Thanks.

you have to avoid using splice because it modifies the current array and returns that, you have to find a way to do it without splice

Yep, I knew it… Thanks.

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