📖 Scenario: Imagine a line at a coffee shop where customers wait their turn to order. The first person to join the line is the first to be served. This is called the FIFO (First In, First Out) principle, which is exactly how a queue works in programming.
🎯 Goal: You will create a queue using a JavaScript array, add customers to the queue, remove the first customer served, and then display the current queue to understand how FIFO works.
📋 What You'll Learn
Create an empty array called queue to represent the queue.
Add three customers named exactly 'Alice', 'Bob', and 'Charlie' to the queue in that order.
Create a variable called servedCustomer to hold the customer who is served first.
Remove the first customer from the queue using the FIFO principle and assign it to servedCustomer.
Print the servedCustomer and the current state of the queue.
💡 Why This Matters
🌍 Real World
Queues are used in real life for waiting lines, like at banks, stores, or call centers, where the first person to arrive is served first.
💼 Career
Understanding queues and FIFO is important for software developers working on task scheduling, print jobs, customer service systems, and many other applications.
Progress0 / 4 steps
1
Create an empty queue
Create an empty array called queue to represent the queue.
DSA Javascript
Hint
Use [] to create an empty array and assign it to queue.
2
Add customers to the queue
Add three customers named exactly 'Alice', 'Bob', and 'Charlie' to the queue in that order using the push method.
DSA Javascript
Hint
Use queue.push('Alice') to add 'Alice' to the end of the queue.
3
Serve the first customer
Create a variable called servedCustomer and assign it the first customer removed from the queue using the shift method.
DSA Javascript
Hint
Use queue.shift() to remove the first customer from the queue and assign it to servedCustomer.
4
Print the served customer and current queue
Print the servedCustomer and then print the current state of the queue.
DSA Javascript
Hint
Use console.log(servedCustomer) to print the served customer and console.log(queue) to print the remaining queue.
Practice
(1/5)
1. What does FIFO stand for in the context of a queue?
easy
A. First In, First Out
B. First In, Final Out
C. Fast Input, Fast Output
D. First Input, First Output
Solution
Step 1: Understand FIFO meaning
FIFO means the first element added is the first one to be removed.
Step 2: Match the acronym
First In, First Out exactly matches FIFO.
Final Answer:
First In, First Out -> Option A
Quick Check:
FIFO = First In, First Out [OK]
Hint: Remember queue removes in order added, first comes out first [OK]
Common Mistakes:
Confusing FIFO with LIFO (stack)
Thinking FIFO means last in first out
Mixing up acronym words
2. Which of the following is the correct way to add an element 5 to a queue named q in JavaScript using an array?
easy
A. q.unshift(5)
B. q.pop(5)
C. q.shift(5)
D. q.push(5)
Solution
Step 1: Understand queue insertion
In a queue implemented with an array, new elements are added at the end.
Step 2: Identify correct method
Array method push adds element at the end, so q.push(5) is correct.
Final Answer:
q.push(5) -> Option D
Quick Check:
Use push to add at queue end [OK]
Hint: Use push to add, shift to remove in JS queue [OK]
Common Mistakes:
Using pop to add (pop removes last)
Using shift to add (shift removes first)
Using unshift adds at front, not end
3. What will be the output of the following JavaScript code?