Stand in Line - Is my explanation correct?

Tell us what’s happening:

I couldn’t make the code by my own so I watched the explanation video and copied the code, but I think I manage to understand the code afterwards, which I tried to explain in the comments. I know it’s very long and repetitive, but it’s to make sure I can understand it later.

Can someone tell me if it is wrong?

Your code so far

//this function called nextInLine takes the parameters "arr" and "item" (they're just names, you can change them as long as you change the rest of the code to match)
 function nextInLine(arr, item) {

//this will take whatever is put inside the "arr" parameter (testArr in line 23), and push (which inserts data at the end of the array) whatever is inside "item", and because "item" is the name of the second parameter of the function, "item" will be changed to the second value you use in the function call ("6", in line 23)

//this will remove the first item of the array (shift) and then return that number, and this return value can be obtained by calling the function, which is done at line 23 (resulting in the value 1 at the second line of the console)
  return arr.shift();


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

//transforms the array into a string
console.log("Before: " + JSON.stringify(testArr));

//nextInLine will have "testArr" as parameter1 (called arr) and "6" as parameter2 (called item). By calling the function, it will do whatever you coded the function to do. In this case it will push the number 6 (which is inside the parameter "item" right now) in the array testArr (which is inside the parameter "arr"), resulting in testArr being [1,2,3,4,5,6] (you can test this by commenting line 8 and checking the third console line)

//After running line 5, line 8 will remove (with the shift command) the first item of the array (which in this case will be testArr), and return that value, which is "1" in this case

//In the end, this line will basically call the function, the function will do whatever you coded, and will then return a value, in this case "1" (because of what is coded in line 8)
console.log(nextInLine(testArr, 6));

//Because you now changed the array "testArr" by calling the function, the values inside it changed accordingly  
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/ Safari/537.36 Edg/118.0.2088.76

Challenge Information:

Basic JavaScript - Stand in Line

1 Like

I’d really strongly recommend not copying answers. As you move on through the JavaScript courses, “figuring out” the code is much more important than the actual answer. We’re here to help you figure out the code without looking up answers.

Your description is indeed long but looks accurate to me!

1 Like

I really tried to not use it, but the hint and forum answers didn’t really help, so I made sure to at least understand it correctly afterwards. But I’ll try my best to never copy again, thanks for the help :slight_smile:

1 Like