Question about Refactor-global-variables-out-of-functions

Tell us what’s happening:
I passed this challenge, but there are things I still don’t understand.

  1. why the first parameter (array) that I passed to the add function function add (array, bookName) { } represents the array bookList?
    I say “represents” because when I wanted to make a new copy of the bookList variable I used the spread operator with it => let newArr = […array];
    Same question about the remove function.

  2. my second question is the second parameter of the add and remove functions, Bookname. From what I understood, bookName represents the indexes of our bookList array. And as a beginner, I thought that to call the functions and pass them arguments we must write for example: add(arg1, arrg2). Here I saw that new variables have been defined (newBookList, newerBookList, newestBookList), and the argument pass from there represents the second parameter of the function add() and remove(). I don’t understand this.

I just want to clarify that as a beginner I sometimes spend hours on a challenge. Once I pass the challenge, strangely enough, I don’t understand how the code I write works. :sob:

   **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"];

// Change code below this line
function add (array, bookName) {
 let newArr = [...array];
 newArr.push(bookName)
 return newArr;
 //return bookList; 

 // Change code above this line
}

// Change code below this line
function remove (firstArr, bookName) {
 let newArr2 = [...firstArr];
 if(newArr2.indexOf(bookName)>= 0) {
   newArr2.splice(newArr2.indexOf(bookName), 1);
 }
  return newArr2;
   // Change code above this line
}


var newBookList = add(bookList, 'A Brief History of Time');
var newerBookList = remove(bookList, 'On The Electrodynamics of Moving Bodies');
var newestBookList = remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');
//console.log(bookList);
console.log(newerBookList);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36.

Challenge: Refactor Global Variables Out of Functions

Link to the challenge:

Hi @Ndiaye !

I have edited your post to include spoiler tags for those who have not worked on this problem yet.


At the end of your code, there are function calls like this

add(bookList, 'A Brief History of Time');

and the first argument is the booklist array.

I am not sure I understand your second question.
But I just view bookName as a string that gets added or removed from the array.
If we look at the function calls at the bottom, the second arguments are just strings.

add(bookList, 'A Brief History of Time');

Hope that makes sense!

1 Like

Thank you, I get what you mean.
But this also is a fonction call ?

remove(add(bookList, ‘A Brief History of Time’), ‘On The Electrodynamics of Moving Bodies’);

we’re calling the fonction remove and the function add in the same line, aren’t we ?
little bit confused.

Yes this is a function call.

It will be easier to understand when it is broken down.

So the remove function takes in two parameters.

function remove (firstArr, bookName)

In this function call,

remove(add(bookList, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');

Here is the first argument

add(bookList, 'A Brief History of Time')

which returns an array.

and here is the second argument

'On The Electrodynamics of Moving Bodies'
1 Like

Thank you very much ! I can see better.
the first argument of this function call was also a function call in the first time
var newBookList = add(bookList, 'A Brief History of Time');
:neutral_face:

1 Like

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