Stand in Line (Confused on what is actually going on here)

Stand in Line (Confused on what is actually going on here)
0.0 0

#1

Tell us what’s happening:
I’m just having trouble understanding exactly what is going on with this problem (I already found the solution on the forum).

For example:

  1. Where is the variable “arr” declared? Is it defined behind the scenes already?
  2. If the var “testArr” is involved, how can it be worked with if it isn’t anywhere inside the function?

I just feel like I’m missing a key piece of information here. Any insight would be greatly appreciated.

Thank you,
Branden

Your code so far

function nextInLine(arr, item) {
  // Your code here
  arr.push(item);
  var removed = arr.shift();
  return removed;  // Change this line
}

// Test Setup
var testArr = [1,2,3,4,5];

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine([2], 1)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36.

Link to the challenge:


#2

That’s an argument variable. It is passed into the function by the function call.
When we do nextInLine([2], 1) that is "Call the function nextInLine with [2] as arr and 1 as item.

It’s a global variable. Global Scope and Functions


#3

Thank you for responding…this does help fill in some knowledge gaps. However, I’m still confused about the purpose of this argument variable arr.

For example, let’s say I declared an array, arrEx:

arrEx = (1, 2, 3 , 4);

I then would understand if there was a command such as:

arrEx.push(5)

I would expect it to push the number 5 at the end of the existing array, therefore becoming (1, 2 ,3, 4 ,5).

So this is why it’s difficult for me to grasp the idea that I am pushing a value onto this argument variable arr, because I have no idea what arr is in the first place.

Maybe I’m being confused by this:

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

Because when I read the instructions to push a number and return the removed number, I’m thinking it has to do with this sequence of numbers.

But I can obviously see I’m missing the point of this exercise, because I can sense that this is more complicated than simply the .push or .shift function.


#4

The function nextInLine has two parameters (arr, item). When you make a call to the function such as:

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

The global variable testArr gets passed as the first argument which is represented by arr inside the function. The value 6 gets passed as the second argument which is then represented by item inside the function. When you push a value into arr, you are actually pushing the value into testArr, because testArr was passed into the function.