Stand in Line Array Question

How does arr.push (item); know what array to push.
Is it just guessing it must be the only array in the script?
What happens if there were 2 arrays?

  **Your code so far**

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


}

// 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 (iPhone; CPU OS 14_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/33.0 Mobile/15E148 Safari/605.1.15.

Challenge: Stand in Line

Link to the challenge:

I don’t understand your question. Inside of the scope of the function nextInLine there is only one array, arr.

arr isnt any array in the script, its the array with the name arr. The function nextInLine has parameter called arr. This parameter represents the array which the push method is applied to. You could change the whole function parameter naming and it would still produce the same result, for example:

function nextInLine(thisIsArray, item) {
   thisIsArray.push(item);
   return thisIsArray.shift();
}

In this example the push method is applied to the thisIsArray argument/variable, which should be represented by an array when calling the function

How does it know to push into the
var testArr?

Because the function is called with testArr as the argument.

testArr is the name of a variable.
That variable has an array assigned to it.
So testArr refers to one specific array in the program.
Each specific array in JavaScript has functions attached to it that let you do useful things.
One of those functions is called push.
So testArr.push(4) would be applying the push function to testArr.

arr is the argument to a function.
It will also be an array, maybe the exact same one as testArr, but where it’s defined, that’s a placeholder:

function nextInLine(arr, item) {
  arr.push(item);
  return arr.shift();
}

When you call the function:

nextInLine(testArr, 6)

What it does is, inside the function, assign whatever testArr is ([1,2,3,4,5]) to the variable arr, and assign 6 to the variable item. Then it runs the code:

[1,2,3,4,5].push(6);
// `arr` is now [1,2,3,4,5,6]
return [1,2,3,4,5,6].shift();

If you run the function with a different array, it will assign whatever that array is to arr,
and whatever the second argument is to item, and run the function on that.
The arr and item variables only exist inside the function, and get reassigned each time the function is ran

1 Like

I now understand something else is happening in the display section.
Which is beyond my understanding at present.
I can see that nextInLine and testArr are being called in the 2nd line of the display section.
My mind is now at ease because I thought I was missing something fundamental.
Thanks everyone

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