Basic JavaScript - Stand in Line (Passed but need deeper understanding)

I was able to pass the challenge and I figured out early on that .push and .shift were needed. The part that I don’t understand is how it was able to work with arr and item seemingly being undefined. arr.push(item) gives 6, but where did the 6 come from?

the Setup and Display code were also confusing aspects and don’t provide hints. testArr is a different variable name than arr. I’m quite confused about where the numbers are being drawn from.

Overall I feel like this was poorly explained and presented. I had to use the video for a solution in spite of knowing .push and .shift were the answer.

Challenge: Basic JavaScript - Stand in Line

Link to the challenge:

We would need to see the code to help.

Here is info on how to enter code into the thread:

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

And if it is a working solution, please put [spoiler][/spoiler] tags around all that.

I opted not to share the code this time since I passed the challenge, but I do need a deeper explanation about the pre-written portion. As such, clicking the link provides all the code in question.

Thanks for the formatting advice, especially the spoiler tags. I’ll keep it in mind when posting in the future.

It’s really hard to explain without the code

There are different approaches to solving these and different flavors of these approaches. Without reference to specific code, it’s very hard to help. It would be like saying to someone, “explain this poem to me, it’s about flowers”. We have to see the poem to be able to help.

Code is here

function nextInLine(arr, item) {
  // Only change code below this line 

  return  arr.shift();

  // Only change code above this line

// Setup
let 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));

So to reiterate, I’m really confused about how nextInLine comes up with any numbers given that it doesn’t explicitly contain any. arr and item haven’t been defined as far as I can tell. So how does arr.push(item) manage to push 6 on an undefined set?

What does this line do?

1 Like

I rewrote your code in a more standard format - spoilers around the whole code - easier to read.

1 Like

Thank you, I noticed last minute that the spoiler tags didn’t work immediately so that was my poor-man’s workaround. Much appreciated.

No biggie, this is how we learn.

I suppose if I look at it hard enough that it prints out the function nextInLine with the testArr and 6.

Does that mean arr gets substituted for testArr and Item is substituted for 6? I suppose that makes sense now. It’s still really hard to put it all together though, especially after hiding it between things like "Before: ", JSON.stringify, and "After: ". None of those have been covered in the course yet and makes the challenge poorly presented.

Seeing it all together now though, I’ll have to chew on it a bit more. It feels so strange to almost have to read the challenge from the bottom up to really understand it.

Yeah, sometimes this stuff takes time to ‘jell’ in your mind.

It’s the job of the function call to 1) provide the values of the arguments and 2) determine what to do with the return value.

Yeah it’s sunk in now. I still think it could have been presented much better.

The //Setup (as in testArr) should have been at the top of the page. since we don’t have multiple arrays, only testArr, arr itself is unecessary. Instead it could have read like:

let testArr = [1, 2, 3, 4, 5];

function nextInLine(testArr, item){}

and this would have been much simpler and intuitive rather than hiding important information in between concepts have haven’t been covered yet. Because they weren’t covered yet, I basically didn’t look at that line at all and thought I wasn’t supposed to. It doesn’t help that there’s also a big fat comment that basically reads DO NOT TOUCH as well. Anyway, I guess I’m just ranting at this point. Thanks for finding the missing link!

That would have actually been worse. That would imply that the second argument must be exactly and only testArr, while the second argument can be literally any array.

The idea of function arguments has been covered already, but its easy to misunderstand and takes a while to get used to.

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