Refactor Global Variables Out of Functions - Bug? And double let

Refactor Global Variables Out of Functions - Bug? And double let
0

#1

Tell us what’s happening:
Hey I think there’s a bug in this assignment. The function “add” works fine but my remove function keeps getting the error : "indexOf is not defined (this gets printed 4 times) After going back an forth on this problem my function is nearly identical to the solution given in the Get a hint section:

function remove (arr, bookName) {
  
  let changedArr = [...arr];

  if (changedArr.indexOf(bookName) >= 0) {
    
    changedArr.splice(changedArr,indexOf(bookName), 1);

    return changedArr;
    // Add your code above this line
    }
}

Is this a bug or am I missing something?

I tried running the given solution and it runs without problems. Another thing I’m having trouble understanding is that the let variable gets declared twice in the Get a hint solution, which shouldn’t be allowed? : The console runs both functions add and remove with different parameters. In “add” a new let variable called newArr gets created. Rule of a let is that there can be only 1 let declared with the same name. So when the remove function gets run and also creates a let with the name newArr, shouldn’t the console print an error? Wouldn’t two lets in different functions also be problematic?

I passed the test by copying + pasting the solution, but ideally I would love to understand these problems for learning purpose ^_^;

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 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) {
  
  let changedArr = [...arr];

  if (changedArr.indexOf(bookName) >= 0) {
    
    changedArr.splice(changedArr,indexOf(bookName), 1);

    return changedArr;
    // 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/69.0.3497.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


#2

changedArr.splice(changedArr,indexOf(bookName), 1); You have a typo in this line.