Skip to content

Lesson Review Stand In Line

Rafael J. Rodriguez edited this page Jan 6, 2017 · 2 revisions

Author

@Rafase282

Created by Rafase282

Github | FreeCodeCamp | CodePen | LinkedIn | Website | E-Mail

Details

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.

Useful Links

Problem Explanation:

  • 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 array arr
  • Use an array function you learned to remove the first element from array arr
  • Return the element removed

Hint: 1

  • push adds an item to the end of an array.

Hint: 2

  • shift removes the first element of an array. It also gives that element back to you.

Hint: 3

  • The function queue uses arr and item. 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.

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

Code Solution:

function queue(arr, item) {
  // Your code here
  arr.push(item);
  return arr.shift();;  // Change this line
}

Code Explanation:

  • 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]. 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

Getting Started

  1. Welcome!
  2. Contact
  3. Get Started with Free Code Camp

Front End Development Certification

  1. HTML5 and CSS
  2. Responsive Design with Bootstrap
  3. Gear up for Success
  4. jQuery
  5. Basic JavaScript
  6. Object Oriented and Functional Programming
  7. Basic Algorithm Scripting
  8. Basic Front End Development Projects
  9. Intermediate Algorithm Scripting
  10. JSON APIs and Ajax
  11. Intermediate Front End Development Projects
  12. Claim Your Front End Development Certificate

Data Visualization Certification

  1. SASS
  2. React
  3. React Projects
  4. D3
  5. Data Visualization Projects
  6. Claim Your Data Visualization Certificate

Back End Development Certification

  1. Upper Intermediate Algorithm Scripting
  2. Automated Testing and Debugging
  3. Advanced Algorithm Scripting
  4. AngularJS (Legacy Material)
  5. Git
  6. Node.js and Express.js
  7. MongoDB
  8. API Projects
  9. Dynamic Web Applications
  10. Claim Your Back End Development Certificate

Full Stack Development Certification

  1. Greefield Nonprofit Project 1
  2. Greefield Nonprofit Project 2
  3. Legacy Nonprofit Project 1
  4. Legacy Nonprofit Project 2
  5. Claim your Full Stack Development Certification

Coding Interview Preparation

  1. Whiteboard Coding Interview Training
  2. Critical Thinking Interview Training
  3. Mock Interview 1
  4. Mock Interview 2
  5. Mock Interview 3
Clone this wiki locally