Refactor Global Variables Out of Functions2

Refactor Global Variables Out of Functions2
0

#1

Tell us what’s happening:
As i understand the task says do not change the global variable bookList and I assigned that variable to another inside a function but I am getting (newArr.indexOf is not a function) error

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 (bookL,bookName) {
   let newArr=bookL;
  return newArr.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 (bookL,bookName) {
   let newArr=bookL;
  if (newArr.indexOf(bookName) >= 0) {
    
    return newArr.splice(0, 1, bookName);
    
    // 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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 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

Can you edit your post to explain what you are having trouble with understanding?


#3

I have edited my question


#4

What you are not understanding is when you write the following in your add function, that you are not creating a copy of bookL and assigning it to newArr. You are actually assigning a reference to the same array bookL refers to. You should review previous lessons on how to make a shallow copy of an array.

let newArr=bookL;

#5

thanks I found my error and i solved it

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 (bookL,bookName) {

	 let newArr=[...bookL];
 	  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 (bookL,bookName) {
	console.log(bookL);
	let newArr=[...bookL];

  if (newArr.indexOf(bookName) >= 0) {
    newArr.splice(newArr.indexOf(bookName), 1)
    return newArr;
    
    // 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);

#6

That is great that you solved it, though it is not necessary to post your working solution. I promise I would believe you if you just told me you solved it.

I have blurred out your solution, to avoid spoiling it for someone else.


#7

Thanks one more time, I have almost forgotten how to copy an array with using spread and how to use splice, I reviewed them.