Refactor Global Variables Out of Functions:can't find the problem

Refactor Global Variables Out of Functions:can't find the problem
0

#1

Tell us what’s happening:
When running the code it shows the error bookListCopy.indexOf is not a function. Any reason why?

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


#2

I’d be careful of what you’re returning with bookListCopy.push() Are you sure you are returning what you think you are returning?


#3

the point its your add function return is not array . it’s booklist count;

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

change to

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

#4

One thing is that your code is still updating the global variable bookList, so the tests may fail due to not finding a book in the array or the splice emptying the array. The functions when called should work with a copy of bookList; passing in bookList as an argument does not make a copy in and of itself.


#5

Guys please help I am not able to solve the problem.


#6

I’d be careful of what you’re returning with bookListCopy.push()

This quote by @zapcannon99 is what you need to think about

push doesn’t return the array, it returns its length, which is a number


#7
  1. Make a modification of this function to accept one additional argument. Put this argument before bookName. Or alternatively use arguments[0] since this ain’t global.
  2. Make a copy of arguments[0] by using splice() method.
  3. Find bookName location i.e index in arguments[0] copy by using indexOf() method. For this you need just to put first parameter which have to be zero(0). This is how you say: Make me a copy starting from zero index until it’s ends."
  4. Then you need to use splice(0 one more time, apply it on arguments[0] copy. As a first par. use previously found index of bookName, use this also as a second parameter. This is you saying; “Start cutout this where it starts. End cutout where it ends.”.
  5. Return what is left.