Issue on Refactor Global Variables Out of Functions

Tell us what’s happening:

I put the code below but it doesn’t work whereas i think it should work !

can someone tell me why it doesn’t work?

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) {
let arr = […bookList];
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 (bookList.indexOf(bookName) >= 0) {
let arr = […bookList];
arr.splice(arr.indexOf(bookName), 1);
return arr;

// Add your 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);


// 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) {
  let arr = [...bookList];
  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 (bookList.indexOf(bookName) >= 0) {
    let arr = [...bookList];
    arr.splice(arr.indexOf(bookName), 1);
    return arr;
    
    // Add your 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);

Your browser information:

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

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

One issue I see is how you are using your input parameters.
You are given ‘arr’ and ‘bookName’ but you basically ignore ‘arr’ and create a different variable ‘arr’ with the global variable bookList.

If you notice, this exercise is about not using global variables like bookList.

Instead take another look at the parameter arr and what is in it. Is it something you can use?

I solved this way:

// Add your code below this line
function add (arr, bookName) {
  let newArr = [...arr];
  newArr.push(bookName);
  return newArr;
  
  // 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) {
    let newArr = [...arr];
    newArr.splice(newArr.indexOf(bookName), 1);
    return newArr;
    
    // Add your code above this line
    }

}

for this challenge there is no need to check if the bookName is in the list as you can assume it always will be.

but for the sake of argument, if we look at your if statement, and ask ‘what happens if the book is not in the list?’ What will our answer be?

Your code is missing that logic…
(but again, you don’t need that logic right now , so I would just drop the if statement entirely)

1 Like