What's wrong Refactor Global Variables Out of Functions

Tell us what’s happening:

So, I’m not getting the correct output. Please, clarify the instructions for me. I am passing two different arrays for two different output which I assume they want since adding "A brief history in time doesn’t appear twice in the list. But my outputs are not correct.

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

/* 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
function add (arr, bookName) {
 
   arr.push(bookName);
   return arr;
  
  // 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 (arr,bookName) {
  if (arr.indexOf(bookName) >= 0) {
    
     arr.splice(arr.indexOf(bookName), 1);
     return arr;
    // Add your code above this line
    }
}
var arr=[];
arr=[...bookList];
var arr1=[];
arr1=[...bookList];

var newBookList = add(arr, 'A Brief History of Time');
var newerBookList = remove(arr1, 'On The Electrodynamics of Moving Bodies');
var newestBookList = remove(add(arr1, 'A Brief History of Time'), 'On The Electrodynamics of Moving Bodies');
//var newestBookList=[...arr];

console.log(bookList);
console.log(arr);
console.log(newBookList);
console.log(newerBookList);
console.log(newestBookList);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/refactor-global-variables-out-of-functions

In the functions, you’re pushing and spliceing the input array. The input array is the global array of books, and both of those methods mutate, so what you’re doing is just modifying the global variable. You have copied the array outside of the functions, but that’s no use, the challenge is asking you to do it inside the functions

Yes, i copied the array outside of the function but then I passed it as an argument, so It should be fine. I thought the idea was not to mutate the global array bookList.

Yes, the idea is to not mutate it, but both of your functions mutate the global array: they take the array, mutate it then return the mutated version.

Yes, but the second call to remove has a call ;to the add function which returns an array, so therefore it must take an array as an argument.

You need a local copy of the argument inside the function, so you don’t Change what is passed inside it

Right, but the point of this exercise is that you have two functions that take an array, do something to it, and then return a new version of the array (a copy with some modification). What you are doing is passing a new array into the function, which mutates that array.

1 Like

your var arr=[]; line does, in fact, create a new global array called arr, which is then being passed into your functions. BUT… functions, when receiving an array or an object as a parameter, don’t pass the VALUE, rather they pass a reference. So you’re cloning on global array into another global array, then modifying that.

Functional programming, however, takes in a value (string, array, object) and DOESN’T change that one. Instead, it passes back a new value. So if your functional programming assignment takes in an array, as this one does, it should INTERNALLY create a copy of that array, and do any mutation on that, finally returning that. The original value remains untouched, and your function returns a completely new array.

Thank you for your comments. I got it.

Thank you for the comment. i got it.