📖 Scenario: Imagine you have a stack of books on your desk. You can only add or remove the top book. This is how a stack data structure works using the Last In, First Out (LIFO) principle.
🎯 Goal: You will create a stack using a JavaScript array, add books to it, remove the top book, and then display the current stack.
📋 What You'll Learn
Create an empty stack using a JavaScript array called stack.
Add three books to the stack in this order: 'Book1', 'Book2', 'Book3'.
Create a variable called removedBook to store the book removed from the top of the stack.
Remove the top book from the stack using the correct stack operation.
Print the removedBook and the current state of the stack.
💡 Why This Matters
🌍 Real World
Stacks are used in real life for things like undo buttons in apps, browser history, and managing tasks where the last added task is done first.
💼 Career
Understanding stacks helps in software development, especially in algorithms, parsing expressions, and managing function calls.
Progress0 / 4 steps
1
Create an empty stack
Create an empty array called stack to represent the stack.
DSA Javascript
Hint
Use [] to create an empty array and assign it to stack.
2
Add books to the stack
Add 'Book1', 'Book2', and 'Book3' to the stack using the push method in this exact order.
DSA Javascript
Hint
Use stack.push('Book1') to add 'Book1' to the stack.
3
Remove the top book
Create a variable called removedBook and assign it the value returned by removing the top book from the stack using the pop method.
DSA Javascript
Hint
Use stack.pop() to remove the top book and assign it to removedBook.
4
Print the removed book and current stack
Print the value of removedBook and then print the current state of the stack using console.log.
DSA Javascript
Hint
Use console.log(removedBook) and console.log(stack) to print the results.
Practice
(1/5)
1. What does the LIFO principle mean in the context of a stack?
easy
A. Elements are removed based on their value
B. First In, First Out - the first element added is the first to be removed
C. Elements are removed randomly
D. Last In, First Out - the last element added is the first to be removed
Solution
Step 1: Understand the LIFO principle
LIFO means the last element you put in is the first one you take out.
Step 2: Apply to stack behavior
Stacks follow LIFO, so the newest item is removed first.
Final Answer:
Last In, First Out - the last element added is the first to be removed -> Option D
Quick Check:
LIFO = Last In, First Out [OK]
Hint: Remember stack like a stack of plates, last added removed first [OK]
Common Mistakes:
Confusing LIFO with FIFO
Thinking elements are removed randomly
Assuming removal depends on element value
2. Which of the following is the correct way to push an element '5' onto a stack named stack in JavaScript?
easy
A. stack.insert(5);
B. stack.add(5);
C. stack.push(5);
D. stack.enqueue(5);
Solution
Step 1: Recall JavaScript stack operations
JavaScript arrays use push() to add elements at the end (top of stack).
Step 2: Match method to stack push
push() adds element to stack top, so stack.push(5); is correct.
Final Answer:
stack.push(5); -> Option C
Quick Check:
Push method adds element to stack [OK]
Hint: Use push() to add elements to stack in JavaScript [OK]
Common Mistakes:
Using add() or insert() which are not array methods
Confusing enqueue() with queue operation
Trying to use non-existent methods
3. What will be the output of the following JavaScript code?