Refactor Global Variables Out of Functions - 7/13/2018

Refactor Global Variables Out of Functions - 7/13/2018
0

#1

Tell us what’s happening:
Hey fellow campers,

Been going through the old posts and this seems to be the answer. Not sure if I’m missing something here or it’s bug.

Any ideas?

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 (bookList, bookName) {
  let bookCopy = [...bookList];

  return bookCopy.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 bookCopy = [...bookList];
  let index = bookCopy.indexOf(bookName);

  if (index >= 0) {
    
    return bookCopy.splice(index, 1);
    
    // Add your code above this line
    }
    
    return bookCopy;
}

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/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

Read the documentation for the push method. It has a return value when called, but it is not what you think it is. So when you return the result of the what is returned from the push method, you are returning something that is no longer an array.

Read the documentation for the splice method. It has a return value when called, but it is not what you think it is. So when you return the result of the what is returned from the splice method, you are returning an array, but not the array you think.


#3

Thanks! A second pair of eyes is really helpful with coding.