Stand in Line questions

I have a few questions about the stand-in line function…

  1. how is it that the arr function is being called to push the item, but not nextInLine or item argument?

  2. How is arr.push(item); return arr.shift() synching with `var testArr = [1,2,3,4,5];?

link to the lesson i am talking about: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/stand-in-line
`

With this line

console.log(nextInLine(testArr, 6));

the function nextInLine(arr, item) is called.
Because the call to the function is nextInLine(testArr, 6) you have access to testArr.
If you use the statement arr.push(item); inside the function it will be interpreted as testArr.push(6);. Basicly arr becomes a reference to testArr until the function is done. In this way you can use the same function for multiple arrays without repeating code.

1 Like

Can you explain it a little bit more? Like I know how push and shift and stuff? but i do not undertsand how they work.

Are you comfortable with functions?

This is a function definition. The name of the function is nextInLine, and it takes two parameters - arr, and item. This code is not run until the function is called.

function nextInLine(arr, item) {
  // ...
}

This is a function invocation. The code inside the function definition will run with the value of arr set to reference testArr, and the value of item as 6.

nextInLine(testArr, 6)
2 Likes

thank you @exari and @colinthornton for explaining this to me. I think I finally understand it.

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