Stand in Line confusing me!

Hi guys, I have a little don’t understand why

arr.shift(); 
return arr.shift();

The result with console.log return 2.

But when I follow the hint and wrap my array in var it return 1 in the console.log

function nextInLine(arr, item) {
// Your code here
    arr.push(item); // at this line, arr equals [1, 2, 3, 4, 5, 6]
    arr.shift(); // at this line, arr equals [2, 3, 4, 5, 6]
  return arr.shift(); // Change this line
}

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

// Display Code
console.log(nextInLine(testArr, 6)); // I get 2 but not 1. Why???
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

Copy and paste your code here and press “visualise execution”
http://pythontutor.com/javascript.html#mode=edit

1 Like

If you want 1 to be returned from the function,

remove arr.shift() in the middle. You are shifting twice in your function and returning shifted value in the end.

1 Like

@shimphillip hanks for your answer! I found the answer too!

http://forum.freecodecamp.org/t/stand-in-line-javascript-exercise-stuck/5103/15

@ilenia Nice tutor platform! Thank you for willing to help. I understand now!