SpoilerAlert /Help/ Refactoring Global Var out of Functions

SpoilerAlert /Help/ Refactoring Global Var out of Functions
0

#1

Good Morning all !

Seems I am still unsure why things went wonky here.

Here is the code :

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

/* This function should add a book to the list and return the list */
// New parameters should come before the bookName one

// Add your code below this line

let myBooks=bookList.slice();


function add (myBooks,bookName) {
  
  return myBooks.push(bookName);
  
  // Add your code above this line
}

/* This function should remove a book from the list and return the list */
// New parameters should come before the bookName one

// Add your code below this line
function remove (myBooks,bookName) { 
  if (myBooks.indexOf(bookName) >= 0) {
    let myIndex=myBooks.indexOf(bookName);
    return myBooks.splice(myIndex,1);
   
    
    // Add your code above this line
    }
}

var newBookList = add(myBooks, 'A Brief History of Time');

var newerBookList = remove(myBooks, 'On The Electrodynamics of Moving Bodies');

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

console.log(bookList);

Now it is complaining about the myBooks.indexOf not being a function. also the console.log (bookList) is not spewing forth the original list. In testing I have moved a console.log(myBooks) around a bit and that all seems fine. until you get to the bottom.

Finally , in checking some of the other solutions available, I noted that for some reason everyone seems to be putting the copy of the array into the function instead of in the global and using that as I have. Why is that ?

Thank you in advance. I am sure its just early morning non-caffinated brain, but thank you for your time. in advance. it is greatly appreciated !


#2

The reason you are getting this error is due to your add function. Most JavaScript methods have a specific return value. Based on how you have written your add function, you must think that push returns an array, but it does not. It returns the length (a number) of the array after the new item is added. This is a problem, because your add function ends up returning a number and newBookList is assigned this number. In the last line (see below), since the add method returns a number instead of an array, the remove method ends up trying to find the index of a number, because mybooks inside the remove function would be a number (the one passed in from the add method).

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

Because that is the entire point of the challenge. You are suppose to refactor any global variables out of the function and only uses values which have been passed into the function. That is the reason why you were NOT supposed to change any code after the comment seen below. Instead, you changed bookList to myBooks.

// Add your code above this line

Try to rewrite your functions to only use the original bookList and do not make a copy outside of the functions. Make the copies inside the functions.


#3

Alright, I am game… let me make a small clarification on my own assumption. I was actually assuming that the PUSH function would just insert the appropriate item to the end of the myBooks array , and that there would be the inference of the entire myBooks array that would be fed to the REMOVE portion of the function. Interestingly this function does work in Codepen, but not here. since it does not , as you correctly say, remove the global variable.

As an abstract, maybe you could clarify for me why one would want to do that if ( and you can correct my thought since if you ask my wife I can actually be wrong LOL ) you use the global variable instead of having that inside the array that the functionality of the program would be more efficient.
I say that with the understanding that the point is that having the variable outside of the function defeats the modularity (?) of the function its self ?

I will take a stab at re-writing this as it was given there are 2 options I looked at the SLICE and the […ARRAY} options that would accomplish that.

Thank you again for taking the time to teach. I greatly appreciate the gift.


#4

It is not a question of efficiency. I think this link does a good job explaining why not to use global variables.