Stand In Line is a CheckPoint where you're supposed to:
Write a function nextInLine
which has two parameters
an array (arr
) and a number (item
).
That part has been done for you here:
function nextInLine(arr, item) { }
With that function declaration you have a function name of nextInLine
.
You have two parameters arr
which represents an array passed into the function and item
that represents a number passed to the function.
A few example function calls:
nextInLine([4,3,2] , 1)
the values passed to arr
is [4,3,2]
and the value passed to item
is 1
nextInLine([2,4,6,8] , 10)
... arr
is [2,4,6,8]
& item
is 10
nextInLine([2,3,5,7,11] , 13)
... arr
is [2,3,5,7,11]
& item
is 13
(first 5 and 6th prime numbers)
nextInLine([] , 0)
... arr
is []
& item
is 0
This part is an example using a Global Variable as a parameter to another function call.
var myArr = [1,3,5,7]; nextInLine(myArr , 9)
... arr
is a reference to the global variable myArr
with the value of [1,3,5,7]
& item
is 9
When you modify arr
inside of your function you're actually modifying the value of the global variable because of the reference passing. Note: This doesn't work like this when you pass primitives.
Parameters are treated like variables
that represent the values that get passed into your function from the function call (arguments).
Again the two parameters for the nextInLine
function are arr
& item
.
Per the instructions: Add the number to the end of the array, then remove the first element of array. The nextInLine
function should then return the element that was removed.
You need to add the number (item
) to the end of an array (arr
).
You also need to remove the first element from an array (arr
).
Then you need to have your function return
the removed element from the array arr
.
// Write a function nextInLine which has two parameters
// an array (arr) and a number (item).
function nextInLine(arr, item) {
// Add the number to the end of the array,
// then remove the first element of array.
// The nextInLine function should
// then return the element that was removed.
return item; // 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));
How do you add a number (item
) to the end of an array (arr
)? What does this method return?
How do you remove the first element from an array (arr
)? What does this method return?
Hint: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
Add Hint: http://www.bennadel.com/blog/1796-javascript-array-methods-unshift-shift-push-and-pop.htm
Parameters are variables that represent the values that get passed into your function from the function call.
Notice how the variables
level
andscore
in the function definitionaddScore
are called parameters.However, when we invoke the function like in:
addScore(3, 10)
oraddScore(6, 20)
the values are called arguments. Here is an important lesson:
You define a function with parameters, you call a function with arguments.
Another example of this:
You can use the
fName
anduName
parameters just like a variable inside of your function.Yet another example code:
You can see how the parameter is used like a variable inside of the function.
And you can do mathematical operations to the parameter and assign the value to the variable
result
.Other important things to remember:
* A function can have zero parameters. You still have to use the parentheses to define it.
* A function might have no return statements. In this case we say that the function returns undefined.
From knowing that please note that technically, calling parameters variables isn't correct. Parameters are part of the function declaration and when the function is called, an execution context is formed and their parameters are variables that hold the passed arguments.