Refactor Global Variables not right

Refactor Global Variables not right
0

#1

Hi!

I have been stuck with this exercise a while already, but I really don’t know what I am doing wrong.

This is the code:

// 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 (bookList, bookName) {
  let newBookList = new Array();
  newBookList = bookList;
  return newBookList.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 (bookList, bookName) {
  let newBookList = new Array();
  newBookList = bookList;
  if (newBookList.indexOf(bookName) >= 0) {
    
    return newBookList.splice(newBookList.indexOf(removedBook), 1);
    
    // 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);

And this is what the test is telling me

// running test
newBookList.indexOf is not a function
newBookList.indexOf is not a function
newBookList.indexOf is not a function
newBookList.indexOf is not a function
// tests completed

I have searched the forum, and I see various answers, but I don’t know what is wrong with mine…


#2

Who told you that

let newBookList = new Array();
newBookList = bookList;

this will create a copy of an array?


#3

I tried the following:

let newBookList = [...bookList];

also tried:

let newBookList = bookList.slice();

Neither seemed to work.


#4

Now, you are on the right step.
Ask yourself what is the return value of splice()


#5

If you mean slice(), I think the return value of slice is the whole array, since I did not specify where to start the slice, it starts from the beginning until the end, and returns a copy of the array.

I did not use splice(), because splice modifies the original array.


#6

The following line sure looks like a splice to me.

return newBookList.splice(newBookList.indexOf(removedBook), 1);

#7

You did use splice() in your code. I’m not talking about the method of shallow copying.

If you’ve answered that, then answer what is the value removedBook in your code.


#8

I thought that gunhoo93 meant the other part, the part he commented on


#9

OHHH It is actually bookName, and not removedBook


#10

To sum up the problems,

  • How to copy an array?
  • What is the return value of splice?
  • What is the value of removedBook?

Once you’ve fixed these problems, you will solve the challenge.

Edit: I also missed one thing. The return value of push()


#11

One last thing. What is the return value of the slice method? Is the following code of your add funtion returning an array or something else?

return newBookList.push(bookName);

#12

I am going to check all your comments and come back.


#13

OK, I am going to answer the problems you detected:

To copy an array, I am doing the following:

let temporaryBookList = theList.slice(0);

Because slice returns a copy of the array. I also changed the parameter’s name just in case.

The return value of splice is the a new bookList, without the book that was specified. It is done by searching the index of the book that was selected, and then eliminate that value. Therefore:

return temporaryBookList.splice(temporaryBookList.indexOf(bookName), 1);

first parameter indicates a number, the index, the second parameter indicates how many elements to eliminate, in this case, 1.

RemovedBook does not exist, it was a mistake, which was replaced by bookName, which is the selected book to remove.

The return value of push is the array + the new book, I mean, it will return a new array with another value.

This is my new code:


var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];


function add (theList, bookName) {
  let temporaryBookList = theList.slice(0);
  return temporaryBookList.push(bookName);

}

function remove (theList, bookName) {
  let temporaryBookList = theList.slice(0);
  if (temporaryBookList.indexOf(bookName) >= 0) {
    
    return temporaryBookList.splice(temporaryBookList.indexOf(bookName), 1);
    }
}

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);

I eliminated the comments to make it shorter.


#14

slice method returns a copy of a new array.

This code, I checked it on the console with the debugger,

return newBookList.push(bookName);

It returns a new Array with an extra value, the selected one, I will make a screenshot.


#15

I think my problem is here, but I don’t understand why. theList says that the value is 5.


#16

The return value of splice is the a new bookList, without the book that was specified

Nope, the return value of splice() is the array of removed items.

let a = [1, 2, 3]
a.splice(0, 1) // [1]
console.log(a) //[2, 3]

The return value of push is the array + the new book

Nope, the return value of push is the length of the array with new items.

var a = [1, 1, 1]
a.push(1) // 4
console.log(a) // [1, 1, 1, 1]

I’m not sure where you are drawing your info from.


#17

Just solved the problem doing this:


var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];


function add (theList, bookName) {
  let temporaryBookList = theList.slice(0);
  temporaryBookList.push(bookName);
  return temporaryBookList;

}

function remove (theList, bookName) {
  let temporaryBookList = theList.slice(0);
  if (temporaryBookList.indexOf(bookName) >= 0) {
  temporaryBookList.splice(temporaryBookList.indexOf(bookName), 1);  
    return temporaryBookList;
    }
}

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);

Could somebody explain why? I appreciate any help

*Nevermind, gunhoo93 answered my question while I was posting this