Functional Programming - Refactor Global Variables Out of Functions

Tell us what’s happening:

Describe your issue in detail here.
I am almost done with my code but I haven’t figured out how I changed the hound code.

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

// Change code below this line
function remove(list, bookName) {
  function remove(list) {
    return list.filer(book => book !== booName);
  }
  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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36

Challenge Information:

Functional Programming - Refactor Global Variables Out of Functions

You don’t want to redefine the function inside of the function. You should not add any more function statements to the code. You are on the right track with adding another parameter to each function which allows you to pass an array into the function. But remember, in JS, arrays are passed by reference, which means that even inside the function you are still manipulating the original array passed in. Instead, you want to manipulate a copy of the array passed in because the instructions are asking you to rewrite the code so that when bookList is passed into the function it is not changed by the function.

You are allowed to completely change the code inside the functions. None of the old code has to remain.

The filter line of code you have would work on its own if you didn’t have typos for it (the method name is filter and the parameter is bookName). That is all you need inside that remove function.

The add book code needs to add the new book to a new copy of the array. There are many ways of doing it. Returning a new array literal with the old array spread in and the new book added to the end might be one option here.

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