# Functional Programming - Refactor Global Variables Out of Functions

Tell us what’s happening:
Ok… so to be honest I would have been stunned if my “work so far” had been sufficient to accomplish the task…
my grasp of the concepts laid out in front of me has felt a little weak since i started this chapter…

I have been waiting for the moment where i would have to post

WHAT AM I MISSING… conceptually… not just help me towards the answer… if i may be so bold as to ask… can someone point out what are the holes… making my “thought boat” all leakkkyy hahaha
idk really how to ask this…
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
book = bookName
let bookList1 = bookList

bookList1.push(book);
return bookList1;

// Change code above this line
}

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

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

// Change code above this line
}
}
``````

User Agent is: `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36`

Challenge: Functional Programming - Refactor Global Variables Out of Functions

Link to the challenge:

When you say “let bookList1 = bookList” in the “add” function, you are performing a reference to that variable. What you have to do is instead copy the array. There are several different ways to do this, but an easy way to remember how to copy an array is to use structuredClone() which will copy the array you pass into it (and works best with simpler arrays).

You also need to put a parameter in each function so that the array can be passed in. Below, I just went ahead and put that passed in array in the first spot and then the book name in the second slot. But, outside of a testing situation, your parameters can be defined in whatever way you wish.

I hope this helps!

``````// 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(passedInArray, bookName) {
//Create a copy of the passed in array: "bookList"
const bookList1 = structuredClone(passedInArray);
bookList1.push(bookName);
return bookList1;

// Change code above this line
}

// Change code below this line
function remove(passedInArray, bookName) {
//Create a copy of the passed in array: "bookList"
const bookList1 = structuredClone(passedInArray);
const book_index = bookList1.indexOf(bookName);
if (book_index >= 0) {

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

// Change code above this line
}
}
``````

thank you very much for taking the time to consider what i was asking… i really appreciate that …and thanx for that structuredClone() bit too

No worries! Happy Coding!

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