-
Notifications
You must be signed in to change notification settings - Fork 341
Lesson Review Stand In Line
Created by Rafase282
Github | FreeCodeCamp | CodePen | LinkedIn | Website | E-Mail
Code by Rafase282, the rest of the information by Carole Anne. About queues
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.
Instructions
Write a function queue which takes an "array" and an "item" as arguments.
Add the item onto the end of the array, then remove the first element of the array.
The queue function should return the element that was removed.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
- Lesson: Manipulate Arrays With push()
- Lesson: Manipulate Arrays With shift()
- Lesson: Passing Values to Functions with Arguments
- Change the code below
//Your Code here
and up to//Change this line
- Take note that you are editing the inside of the queue function
- Use an array function you learned to add the
item
to the end of the arrayarr
- Use an array function you learned to remove the first element from array
arr
- Return the element removed
-
push
adds an item to the end of an array.
-
shift
removes the first element of an array. It also gives that element back to you.
- The function queue uses
arr
anditem
. Those are what the tests will use to pass the arrays and elements they will test with and allows the function to be reusable. Do not hardcode any of the tests inside the function.
Solution ahead!
function queue(arr, item) {
// Your code here
arr.push(item);
return arr.shift();; // Change this line
}
- Pushes item at the end of arr
- Calls shift on arr to get the first item and stores it in removed
- Returns removed
Example Run
- Test
queue([2], 1);
runs - The
queue
function is called.arr
becomes [2].item
becomes 1. -
arr.push(item);
Pushes 1 to [2]. Soarr
is now [2,1] -
var removed = arr.shift();
Removes the first element. Soarr
is now [1]. 2 has been removed and is stored inremoved
-
return removed;
2 is returned
Thanks for visiting, if you like this please feel free to star my repo, follow me or even contact me about contributing as it will be a lot of work and having help would be cool.
- HTML5 and CSS
- Responsive Design with Bootstrap
- Gear up for Success
- jQuery
- Basic JavaScript
- Object Oriented and Functional Programming
- Basic Algorithm Scripting
- Basic Front End Development Projects
- Intermediate Algorithm Scripting
- JSON APIs and Ajax
- Intermediate Front End Development Projects
- Claim Your Front End Development Certificate
- Upper Intermediate Algorithm Scripting
- Automated Testing and Debugging
- Advanced Algorithm Scripting
- AngularJS (Legacy Material)
- Git
- Node.js and Express.js
- MongoDB
- API Projects
- Dynamic Web Applications
- Claim Your Back End Development Certificate
- Greefield Nonprofit Project 1
- Greefield Nonprofit Project 2
- Legacy Nonprofit Project 1
- Legacy Nonprofit Project 2
- Claim your Full Stack Development Certification
- Whiteboard Coding Interview Training
- Critical Thinking Interview Training
- Mock Interview 1
- Mock Interview 2
- Mock Interview 3