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 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