Stand in Line - Am I right?

Tell us what’s happening:

Hey good folks! I was hoping you could tell me if I am thinking of this the right way?
This was giving me some headache. But I think I figured it out.

function nextInLine(arr, item) { // A function named nextInLine. Inside the parenthesis is parameters that act as placeholders for arguments thats defined when we call the function.

arr.push(item); // This one was troubling me. But is it right that here we are saying: Insert the “item” argument at the back of the line?

return arr.shift(); // After inserting the “item” at the back of the line, return the “arr” argument, but at the same time, remove the first item? Otherwise we would have six numbers in the array?


// Test Setup
var testArr = [1,2,3,4,5]; // This is a variable with array

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Here we call the nextInLine and pass the argument "testArr and the number 6. That gives us 2,3,4,5,6 in the “testArr” variable?
console.log("After: " + JSON.stringify(testArr));

Thanks so much!

Your code so far

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

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

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

Your browser information:

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

Link to the challenge:

This one you almost have it. Actually, it is not returning the arr, it is just returning the first item. The .shift() gets the first item and the return statement returns the item.

Thanks so much for your answer @RandellDawson
So does that mean following:

  1. We are passing testArr, wich is 1, 2, 3, 4, 5 into the nextInLine function.
  2. The function takes testArr, and put “item” at the back of the line. (testArr now has six numbers?)
  3. Then it removes and stores the first item (1). (Now testArr is back to five numbers?) And return the first item (1). That means that the number 1 is displayed.
  4. Finally this new testArr is printed to the console.

Does that sound about right?

Yes, you have it!

I revised the function with some extra console.log statements plus I broke the

return arr.shift();

into two parts, so you can really see what is happening.

var first = arr.shift(); // removes the first item in array and assigns it to a new variable
return first;  // return this variable containing the first item of arr

See the following link for my modifications. It is better if you open in on their site, because it is wider and you can see the console easier.

That’s brilliant! Thank you so much for taking the time. I really appreciate it. That modification made it really clear. Great help