Output without change of Value

Output without change of Value
0

#1

Tell us what’s happening:
Well i think i did everything right`… whats my problem here?

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

The instructions say:

Refactor (rewrite) the code so the global array bookList is not changed inside either function

You are passing bookList into these functions. But (and this is a confusing point for some) with non-primitive types, you are not passing the value of the variable but the address of the variable. So, inside the functions, the variable list points to the same memory as bookList - any changes you make to list will also happen to bookList because although they are two different variables, those variables are addresses pointing to the same memory.

You need to copy the data and then alter the copy. In ES5, the standard way to copy an array is with slice, but in ES6 we can easily use the spread operator.


#3

I alterd my code like you said, but it didnt work. :confused:

// 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 (list, bookName) {
  let t = [...list];
  return t.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 (list, bookName) {
  let t = [...list]
  if (t.indexOf(bookName) >= 0) {
    
    return t.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);

EDIT: Got it now i needed first to run the function and then return the value.


#4

OK.

  let t = [...list];
  return t.push(bookName);

push returns the number of items after the push, not the array. You need to return the newly created array. You could put it on its own line and then return, but the ES6 way is thusly:

  return [...list, bookName]

Your delete:

return t.splice(0, 1, bookName);

This would start at index 0, remove one item and add bookName - not what you want. The splice should be for one item, but do you always want it to start at index 0? Think about it. Also, splice does not return the array, it returns the deleted elements, not what you want.

When I fix those two things, it passes.

Your issue is with understanding these prototypes methods. I know it’s confusing - some of them alter the original, some don’t. Some return a new array/object, some return something else. I always double check the docs to make sure.