how does the arr in function nextInLine get assigned to it? You can see below that the var testArr = [1,2,3,4,5]; is an array but how does this arr get entered into the function? Is it only because it is the only arr?
Your code so far
function nextInLine(arr, item) {
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(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36.
testArr is being passed to nextInLine function as arguments in this line:
nextInLine(testArr, 6)
This is how the program flows:
nextInLine(arr, item) { ... }
In this line we create a function called nextInLine and inform it that it will accept two parameters to work with,
Inside its body function ( the { ... } ) those two parameters will be called arr and item.
Mind this is just a name for the function itself… you could have called them anything you’d like it to.
nextInLine(testArr, 6)
Here we are telling to the program to start executing nextInLine and take testArr and 6 as arguments.
Or in other words:
function nextInLine(arr, item) {
// simply out in the console the value of arr and item
console.log('arr is '+ arr)
console.log('item is ' + item)
}
// Test Setup
var testArr = [1,2,3,4,5];
nextInLine(testArr, 6) // arr is [1,2,3,4,5] item is 6
nextInLine is function that is declared with 2 parameters/arguments
function nextInLine (arr, item)
arr is the variable name that refers to the first argument
and item is the variable name that refers to the second argument
when we call the function
nextInLine( [1,2,3] , 10 )
we are calling nextInLine and passing [1,2,3] tothe first argument and 10 to the second argument…
which both will be become a local variable inside the function scope with the name arr and item respectively.
inside the function scope
function nextInLine (arr, item) {
// 'arr' can be used to refer the first argument which is `[1,2,3]`
// and 'item' can be use to refer to the integer `10`
// as both were passed by the function called.
}
so if i try to draw the relation, it would be something like the picture below