Please help me with Refactoring Global Variables Out of Functions

I understand that the code shouldn’t effect the original bookList, so I created a new variable, but beyond that I’m not sure how to solve this. I thought maybe I could google alternatives to push and splice? Thanks

  **Your code so far**

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

// Change code below this line
function add (bookName) {

freshBookList.push(bookName);
return freshBookList;

// Change code above this line
}

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

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

  // Change code above this line
  }
}

var newBookList = add(freshBookList, 'A Brief History of Time');
var newerBookList = remove(freshBookList, 'On The Electrodynamics of Moving Bodies');
var newestBookList = remove(add(freshBookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');

console.log(newBookList);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36.

Challenge: Refactor Global Variables Out of Functions

Link to the challenge:

When you do that, freshBookList will still reference the original array bookList. You need to create a copy of the array. You can read more in the article below.

you need to make functions that do not reference global objects, making a copy (even if that is not a copy) that is still global is not enough

also, check if adding freshBookList to itself is what you want to do

read carefully all challenge description, there is written a lot and you will need to apply it

@nibble thanks.

@ieahleen ah I see, so I was just declaring it as global and fixed it by declaring in the variables. I ended up finding that I had to define a unique property (if that’s the right term) within the function ie function add (originalBookList, bookName) and function remove (originalBookList,bookName).

I don’t remember learning that I could enter any property name I wanted like originalBookList inside the function. Do you know which exercise covered this? These functions are still new to me, so I haven’t gotten it down yet.

It’s just a function parameter, you learned about that long ago.

Properties are on objects

1 Like

in this challenge it says:

  1. Declare function parameters - any computation inside a function depends only on the arguments passed to the function, and not on any global object or variable.

and

Note: Both functions should return an array, and any new parameters should be added before the bookName parameter.

you met parameters in the second challenge about functions: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/passing-values-to-functions-with-arguments

they should not be that new at this point, if you have still issues with basics such as functions, try to practice more writing them

1 Like