Functional Programming - Refactor Global Variables Out of Functions

Tell us what’s happening:

Hello, I am trying to replicate the array so that i can modify it, but it’s not working. Could someone tell me why? Thanks!

Your code so far

// 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
function add(bookName) {
  const bookL = bookList
  bookL.push(bookName);
  return bookL;
  
  // Change code above this line
}

// Change code below this line
function remove(bookName) {
   const bookL = bookList
  const book_index = bookL.indexOf(bookName);
  if (book_index >= 0) {

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

    // Change code above this line
    }
}

Your browser information:

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

Challenge Information:

Functional Programming - Refactor Global Variables Out of Functions

Arrays are objects and objects are passed by reference, not by value, so your bookL variable is referencing the same object in memory as your booklist variable.

Unfortunately, one of the ways to do what you are trying to do is not introduced until later in the course:

Return Part of an Array Using the slice Method

The slice method returns a copy of certain elements of an array. It can take two arguments, the first gives the index of where to begin the slice, the second is the index for where to end the slice (and it’s non-inclusive). If the arguments are not provided, the default is to start at the beginning of the array through the end, which is an easy way to make a copy of the entire array. The slice method does not mutate the original array, but returns a new one.

Hope that helps.

ohh, and also the three dots, thanks!