Refactor Global Variables Out of Functions challenge

Hi guys, how do i pass the remaining of this challenge

  **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(bookList,bookName) {

bookList.push(bookName);
return bookList;

// Change code above this line
}

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

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

  // Change code above this line
  }
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0

Challenge: Refactor Global Variables Out of Functions

Link to the challenge:

I think you should have an argument for your

function remove(bookName) {
const book_index = bookList.indexOf(bookName);
if (book_index >= 0) {

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

function that is an array of books. If not you are mutating the global bookList array.

Edit Splice also mutates an array instead of returning a new array.

function remove (firstArray, bookName) {

const book_index = bookList.indexOf(bookName);

if (book_index >= 0) {

firstArray.splice(book_index, 1);

return firstArray;

first I would backup to the

function add(bookList,bookName) {

bookList.push(bookName);
return bookList;

function. Here you are also mutating the bookList array.

Rewrite the code so the global array bookList is not changed inside either function

Look into making a copy of an array inside your function using the spread syntax

 const newArray = [...array]

and then mutate it before returning it.

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