Stand in Line: assignment of arr variable

Tell us what’s happening:

Is it wrong if I write the push statement as arr = arr.push(item) ? The solution given in hint doesn’t assign it to variable arr.
Your code so far


function nextInLine(arr, item) {
  // Your code here
  arr = arr.push(item);
  var removed =  arr.shift();
  return removed;  // Change this line
}

// Test Setup
var testArr = [1,2,3,4,5];

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/stand-in-line/

The Array.push() command will modify the existing array. As a result, you don’t need to assign it to anything like you would, say, array.slice().

Instead, for yours you can use
arr.push(item);
and it will work properly.

You could even remove your removed variable and instead return the shift, since shift also modifies the existing array:

function nextInLine(arr, item) {
  // Your code here
  arr.push(item);
 return arr.shift();  // Change this line
}

Good luck!

Thanks Jordan for such a quick response. It is clear to me now.

1 Like

No problem!

Just remember, some array methods modify the existing array and some leave it the way it is. It can be hard to remember which ones do what, so you can always do a quick search to find out!

Bookmark the Mozilla Docs on it for future reference material:

Good luck!