Question about "Stand In Line"

I was able to work through this problem, but it seems that there can be two answers:

The code that worked is below:

function nextInLine(arr, item) {
  arr.push(item);     //Add item to the last position in the array.
  
  return arr.shift();  //Return the first element in the new array.
}

However, it seems that this code should work too:

function nextInLine(arr, item) {
  arr.push(item);      //Add item to the last position in the array.

  return arr[0];         //Return the first element in the new array.
}

What am I missing?

What nextInLine is supposed to do is

  1. to take in an element and append it to arr,
  2. remove arr's first element, and
  3. return the removed element.

The second snippet doesn’t remove the first element from arr, it just returns whatever is its first element.

1 Like