-
Notifications
You must be signed in to change notification settings - Fork 2
Challenge Stand In Line
🚩 Remember to use Read-Search-Ask
if you get stuck. Try to pair program 👥 and write your own code 📝
In Computer Science a queue is an abstract data structure where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front of the queue.
Write a function nextInLine
which takes an array (arr) and a number (item) as arguments. 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.
- Change the code below
//Your Code here
and up to//Change this line
. - Ensure that you are editing the inside of the
nextInLine
function. - Use an array function you learned to add the item to the end of the array arr.
- Use an array function you learned to remove the first element from array arr.
- Return the element removed.
- Challenge: Manipulate Arrays With push()
- Challenge: Manipulate Arrays With shift()
- Challenge: Passing Values to Functions with Arguments
The push()
method adds an item to the end of an array.
try to solve the problem now
The shift()
method removes the first element of an array. It also returns the element removed.
try to solve the problem now
The function nextInLine
uses arr and item. Those are what the tests will use to pass the array elements they will test with. It allows the function to be reusable. Do not hardcode any of the tests inside the function.
try to solve the problem now
Solution ahead!
function nextInLine(arr, item) {
// Your code here
arr.push(item);
var removed = arr.shift();
return removed; // Change this line
}
- Pushe item at the end of arr.
- Call the
shift()
method on arr to get the first item and store it in removed. - Return removed.
Example Run
- Test
nextInLine([2], 1);
runs. - The
nextInLine
function is called. arr becomes [2]. item becomes 1. -
arr.push(item);
Pushes 1 to [2]. So arr is now [2,1]. -
var removed = arr.shift();
removes the first element. So arr is now [1]. 2 has been removed and is stored in removed. -
return removed;
2 is returned.
Note: You don't actually need the variable removed. The element removed can be returned directly using return arr.shift();
.
If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:
Thanks @CaroleAnneHannon for your help with Checkpoint: Stand in Line
⚠️ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.- Add an explanation of your solution.
- Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. 🚥
- Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)
See 👉
Wiki Challenge Solution Template
for reference.
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links