"Stand In Line" Confusion

Tell us what’s happening:
I’ve read the forums on this lesson, watched the video, implemented the sample solution (which I understand), and I am “passing” on this lesson, but I am not certain how. Specifically, I do not understand how I am meeting the criteria for conditions 3-5 without the numbers required being returned or implemented in any way. I didn’t want to move on until I understood how I was meeting the criteria.

Thanks.

  **Your code so far**

function nextInLine(arr, item) {
// Only change code below this line
arr.push(item);
var removed = arr.shift();
return removed;
// Only change code above this line


}
// console.log(nextInLine);
// Setup
var testArr = [1,2,3,4,5];

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

Challenge: Stand in Line

Link to the challenge:

What do you think the line above does?

What do you think the line above does?

1 Like

As I am understanding it:

arr.push(item);
this is assigning the parameter “arr” the subsequent value of “item” after it is pushed. In the case of the assigned array from “testArr”, the result would be [1,2,3,4,5,6] since we are adding a value of 6 in the second console.log line.

var removed = arr.shift();
this assigns the variable “removed” the value of “arr” after it is shifted. Since no value is being provided, it will return [2,3,4,5,6]

OK, so if arr is [2] and item is 1, what happens in these same two lines?

1 Like

Now you’ve lost me. Is this a hypothetical, or am I missing where this takes place in the string? (I’m assuming the latter)

Are you asking me if:
function nextInLine([2], 1) {

?

That is exactly what I am asking. You seem to understand the concept, so I was just wanting to make sure you understood the 3rd test.

1 Like

Thanks for clarifying :grin:

Now where I’m getting stuck is how to resolve the interactions with the new parameters. Namely, is the value of “item” simply replaced due to it being an integer, and the value of “arr” being appended due to it being in brackets (treated as an array)? Are both values replaced?

arr and item are just placeholders (function parameters). When the function is called such as: function nextInLine([2], 1) , [2] is an argument passed to arr and 1 is just a value passed to item so you can reference them inside the function. This is just basic function syntax and functionality.

1 Like

Ok, thank you. I think the root cause of my problem was reading the required checks as things I had to implement in the code, rather than checks being run in the background to see if my code was correct.

That being said, I’ll get back to:
function nextInLine([2], 1) {
rather - console.log(nextInLine([2], 1));

This should return 2, because:
arr.push(item); results in “arr” becoming [2, 1]
subsequently:
var removed = arr.shift();
return removed; results in the shifted value from “arr” of 2 being assigned to the variable “removed”

When I create additional entries of:
console.log(nextInLine([5,6,7,8,9], 1));
console.log(nextInLine(testArr, 10));
console.log(testArr[4]);

I see the other criteria are met.

Thank you VERY much for helping me sort this out. I genuinely appreciate it!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.