Refactor Global Variables Out of Functions (Query on using Return)

Tell us what’s happening:
Hi, I have managed to pass the test, but I am curious as to why I cannot do the following code:

function add (arr,bookName) {
  let x = [...arr].push(bookName);
  return x;
  

and

function remove (arr,bookName) {
  let y = [...arr];
  if (y.indexOf(bookName) >= 0) {
    
    return y.splice(y.indexOf(bookName), 1);
    
    // Add your code above this line
    }
}

My impression is that since Return will return the respective values to the calling functions, then it should be okay to write them in a single line?
Why do I need to separate out the declaration of x or y as an array, before I can apply array methods onto them?

What is wrong with doing all together as I: 1) assign them 2) return them altogether?

Thanks in advance.

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 (arr,bookName) {
  let x = [...arr]
  x.push(bookName);
  console.log(x);
  return x;
  
  // 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 (arr,bookName) {
  let y = [...arr];
  if (y.indexOf(bookName) >= 0) {
    y.splice(y.indexOf(bookName), 1);
    return y;
    
    // 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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/refactor-global-variables-out-of-functions/

Every function has a return value. So what is the return value of .push()?

Thanks Jenovs, for the clue. I found out that array.push() only returns the length of the array in Javascript in terms of the mutated array.

In particular, this link has been very helpful in describing why it is so.

So let x = [...arr].push(bookName) actually returns the length of the new array

and return y.splice(y.indexOf(bookName),1); returns the bookName removed, but not the mutated index as intended.

Please correct me if I am wrong.

Yes, .push() returns the length and .splice() returns an array containing removed items (or an empty array if no items were removed).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

1 Like