Stand in Line - Sequence of returned items is confusing me

Tell us what’s happening:

Hi all,

I’ve searched the forum for an answer on this, but to no avail.

After a fair amount of searching, I was able to reach the end of the challenge, but there is a part I don’t understand in two of the examples at the end:

In the highlighted areas, I don’t understand why it does’t return a blank result, based on the following:

When I run console.log, I am applying values of and 5. My understanding of this is that the array should result in a blank value at this stage.

  1. The first step in the function appends ‘arr’ with ‘item’, so, we should end up with blank + 5.

  2. The second step removes the first element of the array, and assigns it to a variable called ‘removed’, so now I have ‘removed’ = blank.

  3. The closing step states that the removed variable is what should be returned when the function is activated (that’s my understandig of it at this early stage in my JavaScript journey).

So, if I am applying value of blank and 5 to the arguments, why does this not return a value of blank? I don’t understand why it returns a value of 5, as I see it like this:

blank + 5 = blank, 5
var removed = blank
return removed

If removed = blank, why does it return a value of 5? Is it as simple as JavaScript realising that the result is blank, so it then defaults to the next item in the list?

Your code so far


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

console.log(nextInLine([], 5));
// 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/75.0.3770.80 Safari/537.36.

Link to the challenge:

Ok first thing, it’s not blank, blank isn’t a thing.

The first step in the function appends ‘arr’ with ‘item’, so, we should end up with blank + 5.

No, it puts item into array. You don’t get “blank + 5”. If there wasn’t anything in the array to start off, it now has whatever item is as it’s only value.

The second step removes the first element of the array, and assigns it to a variable called ‘removed’, so now I have ‘removed’ = blank.

The first element is not a special thing called “blank”. You had an empty list. You put one thing into that list. If you remove the first thing, that first thing has to be, logically, the thing you just put into the list, it can’t be anything else.

Thanks for the speedy response. I understand now, and I see where I was getting the concept mixed up in my head. I was thinking that it was keeping the item and the arr as two separate objects, but essentially adding them together.

So, it’s a case of the item now becoming a part of the array itself. The array was initially empty, so the item that has been added is the first element in the array (because it’s the only element in the array), therefore, that is the value that gets returned as a result.

Cheers for clearing that up!

1 Like