Skip to content

Instantly share code, notes, and snippets.

@revisualize
Last active July 6, 2017 04:08
Show Gist options
  • Save revisualize/ced4a3a6611c6c74bcab34a07eaa4ebf to your computer and use it in GitHub Desktop.
Save revisualize/ced4a3a6611c6c74bcab34a07eaa4ebf to your computer and use it in GitHub Desktop.
The FreeCodeCamp.com Stand in Line challenge with the instructions laid out in comments.

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

@revisualize
Copy link
Author

revisualize commented Oct 10, 2016

Parameters are variables that represent the values that get passed into your function from the function call.
Image
Notice how the variables level and score in the function definition addScore are called parameters.
However, when we invoke the function like in:
addScore(3, 10) or addScore(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:

function hello(fName, uName) {
     return "Hello " + fName + " " + uName + ", How is your day?";
}
hello("Joseph", "@revisualize"); // "Hello Joseph @revisualize, How is your day?"
hello("Bella", "@bellaknoti"); // "Hello Bella @bellaknoti, How is your day?"
hello("Andy", "@dirn"); // "Hello Andy @dirn, How is your day?"

You can use the fName and uName parameters just like a variable inside of your function.

Yet another example code:

function addThree (num) {
    var result;
    result = num + 3;
    return result;
}

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.

@revisualize
Copy link
Author

revisualize commented Oct 11, 2016

The push() method adds one or more elements to the end of an array and returns the new length of the array.

The pop() method removes the last element from an array and returns that element.

The shift() method removes the first element from an array and returns that element.

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment