Issue: Refactor Global Variables Out of Functions

I believe there is an issue with Refactor Global Variables Out of Functions.
I am expecting a single value for the parameter of the add function bookName but instead I am getting the entire bookList passed in. It looks like from the solution that this used to be the case but somewhere along the line this must have been refactored and it is now broken.


// 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) {
  return [...bookList, bookName];
  
  // Change code above this line
}

// Change code below this line
function remove(bookName) {
  return [...bookList].filter(book => book !== bookName);

    // Change code above this line
}

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

Challenge: Refactor Global Variables Out of Functions

Link to the challenge:

1 Like

Per the second point

  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 the challenge instructions

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

You are supposed to add any parameters to avoid using global variables.

1 Like

How am I supposed to know what parameters are being passed to the function?
What if I want bookName to be first?

EDIT: Ah I guess I did read that wrong. Thanks for the info!

Part of the specification

2 Likes

Referring to Solution #1: I still don´t understanding something basic :confused:

How does the code know that we are referring with the first and new parameter (which has to be added according to the incstructions) to the bookList Array? This keeps puzzling me…

Look at where the function we define are being called. You will notice as arguments are provided the bookList and the bookName. Ofc, the way functions work, you could provide any other data with similar structure and they would work the same. That is why it is nice to have any global variables out of functions, so functions are universal and do not affect globals directly

Still not getting it, sorry…

It would be awesome if you could point out in which lesson this was covered as I feel that i urgently need and want to understand how this works.

can you paste the code where you get confused, so i know which bits to comment

Sure, please see the screenshot and the marks. I´m simply not understanding how these two are getting “linked”.

(not sure if this is some kind of spoiler for some so I´m blurring it)



!

The test suite makes multiple function calls, such as add(bookList, "A Brief History of Time")

1 Like

It is called over here

1 Like

Nothing is added to or removed from the bookList array. Both the add and remove functions return a new version of the bookList array.

The way the solution code shows the first 2 function calls is a bit misleading as the return is not used for the next function call.

If you call add using bookList and then call remove using bookList you now have 3 versions of the book list. If you include the last function call you have 4 versions.

  • The original bookList
  • The one where a book was added newBookList
  • The one where a book was removed newerBookList
  • The one where a book was added and removed newestBookList

The lists are completely out of sync which doesn’t really make much sense. If you add or remove a book you have to use the new book list returned as the new state.

1 Like

Now I got it! It totally didn´t pay any attention to the function call … Many thanks for your effort of patience @JeremyLT @Sylvant and lasjorg (sorry for not tagging but I can´t tag more than two people) :slight_smile:

Maybe trying to learn JS at 5 in the morning is not always the best idea :sweat_smile:

1 Like