Basic JavaScript - Stand in Line

i dont understand the stand in line challenge.

arr.push(item);
const removed = arr.shift();
return removed;
this is the code in the hint, but unfortunately i dont still understand. the array defined in the challenge is testArr. why arent they using it for the push and shift but using only arr which is one of the parameters and not an array. i’m kinda slow so dont be pissed if my question appears dumb.

  **Your code so far**
function nextInLine(arr, item) {
// Only change code below this line
testArr.push(item);
testArr.shift(arr);
// Only change code above this line
}

// Setup
const 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/104.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Stand in Line

Link to the challenge:

The only variables that matter inside of this function are the function parameters and any variables you declare inside of the function.

i’m not sure i understand what you mean. why doesnt the defined array matter?

The defined array only matters if the function is called and that array is passed in as a function argument. We don’t want to write a copy of this function for every single possible array we could want to use.

1 Like

thanks. I also noticed the function wasnt called. i understand a bit now maybe. but can you share a link i can read up more about stand in line que?

The function is called right here.

1 Like

Maybe it would help to try to understand the concept of a function first.

Let’s pretend for a second that you are in a line outside of a store. The store has black windows and doors, so you cannot see inside. You are first in-line at the door but it is not your turn yet. Perhaps in this story, you cannot enter the store until a person leaves (there’s a maximum number of people allowed inside due to COVID-19 rules).

so - while you are outside, you are not “seen” by the store yet right? (black windows)
But as soon as someone comes out and you go in, now they see you and greet you.
Great.
A function is the same way as a blacked-out store.
It can only see what is given to it (on the inside of it).

So the function nextInLine for eg is not aware of anything going on below the last } brace (that matches the starting { brace in the definition ).
It can only see 2 things. arr and item.
So it therefore can only do interesting things with arr and item.

(well having said all that, there is also an idea of a global variable but I’m not sure if you learned this yet, but either way, hopefully you understood the idea of the function and its “scope”)

3 Likes

thanks a lot. much clearer now, and i do understand scopes of variables. read about it few days ago

2 Likes

one of the best explanations so far and got me pass this task.
my code after the explanation

Mod edit: solution redacted

1 Like

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like