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.
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
}
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.
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.
Make a copy of arguments[0] by using splice() method.
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."
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.”.