📖 Scenario: You are building a simple program to manage a stack of books. You want to see which book is currently on the top without removing it.
🎯 Goal: Create a stack using an array, then write code to peek at the top book without removing it, and finally print the top book.
📋 What You'll Learn
Create an array called stack with these exact books in order: 'Math', 'Science', 'History'
Create a variable called topBook to hold the top element of the stack
Assign topBook the value of the last element in the stack without removing it
Print the value of topBook
💡 Why This Matters
🌍 Real World
Stacks are used in many places like undo features in apps, browser history, and managing tasks.
💼 Career
Understanding stacks helps in software development roles where data management and algorithms are important.
Progress0 / 4 steps
1
Create the Stack
Create an array called stack with these exact books in order: 'Math', 'Science', 'History'
DSA Javascript
Hint
Use square brackets [] to create an array and separate items with commas.
2
Prepare to Peek
Create a variable called topBook and set it to null for now
DSA Javascript
Hint
Use let to create a variable that can change later.
3
Peek the Top Element
Assign topBook the value of the last element in the stack without removing it. Use stack.length to find the last index.
DSA Javascript
Hint
Remember, array indexes start at 0, so the last element is at length - 1.
4
Print the Top Book
Print the value of topBook using console.log(topBook)
DSA Javascript
Hint
Use console.log() to show the result on the screen.
Practice
(1/5)
1. What does the peek operation do in a stack?
easy
A. Returns the top element without removing it
B. Removes the top element and returns it
C. Adds a new element to the top
D. Checks if the stack is empty
Solution
Step 1: Understand stack operations
A stack stores elements with last-in, first-out order. Peek shows the top element without removing it.
Step 2: Differentiate peek from pop
Pop removes and returns the top element, peek only returns it without removal.
Final Answer:
Returns the top element without removing it -> Option A
Quick Check:
Peek = top element without removal [OK]
Hint: Peek shows top without removing it [OK]
Common Mistakes:
Confusing peek with pop operation
Thinking peek adds elements
Assuming peek checks emptiness
2. Which of the following is the correct syntax to peek the top element of a stack implemented as an array named stack in JavaScript?
easy
A. stack.peek()
B. stack.pop()
C. stack[stack.length - 1]
D. stack.top()
Solution
Step 1: Understand JavaScript array indexing
JavaScript arrays use zero-based indexing. The last element is at index length - 1.
Step 2: Identify correct peek syntax
To peek, access the last element without removing it: stack[stack.length - 1]. stack.pop() removes the element, stack.peek() and stack.top() are not default methods.
Final Answer:
stack[stack.length - 1] -> Option C
Quick Check:
Peek = stack[stack.length - 1] [OK]
Hint: Use stack[stack.length - 1] to peek top [OK]