Stand in Line question about order of the code

Tell us what’s happening:
I looked at the solution to this problem. Why is it that if I declare the variable for the shift before pushing the number result in an error? I have tried moving that same line after the push and it works. I would like to know if there is a logical reason behind this.

Your code so far

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

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

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 7)); // 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/68.0.3440.106 Safari/537.36.

Link to the challenge:

1 Like

I don’t understand the question. What is the code that you tried that causes an error? What is the error?

1 Like

This is it please @Aedallon

arr.push(item); add
  var remove = arr.shift(); remove first element.
  return remove; 

@mrkoodi Instead of giving the OP the answer, why not try understanding the questions posed first. Maybe the OP did not want to see the answer and just wanted some hints and suggestions.

1 Like

Sorry… my bad… it wouldn’t happen again.

This line of code:

var firstNum = arr.shift();

if it is place before:


And I attempt to submit it, it tells me that it is wrong.

If i place

var firstNum = arr.shift();



it is a correct answer. Is there a logical reason for this being the case or is it a bug in the system.

edit: I added the line console.log(firstNum); to see if my code was outputting a different answer, but it seems to be correct. Hence why I would like to know if there is a certain rule that should be followed regarding the order of push and shift.

If you look at the specific tests that you’re failing:

// running test
nextInLine([], 5) should return a number.
nextInLine([], 1) should return 1
// tests completed

You are failing when arr is an empty array. If you shift an empty array, then firstNum is undefined. These tests exists to make sure that you followed the instructions carefully:

Add the number to the end of the array, then remove the first element of the array.