A stack you can look into without disturbing it
A stack is the first structure most frontend work reaches for: the router history you pop back through, the modal layers you close in reverse order, the parser that matches your JSX tags. It is a list where the only item you can touch is the most recent one.
Implement a Stack class. It is constructed with no arguments and starts empty. push(value) adds a value on top and returns the new size as a number. pop() removes the top value and returns it, or returns undefined when the stack is empty. peek() returns the top value without removing it, or undefined when the stack is empty. size() returns how many values are currently held. isEmpty() returns true when nothing is held and false otherwise.
What it has to do
pushreturns the size of the stack after the value was added.popreturns values in last-in-first-out order.popandpeekreturnundefinedon an empty stack rather than throwing.peekleaves the stack unchanged.isEmpty()istrueonly whensize()is 0.
Your workspace
Ready to check it?
5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.
AI Crack & Solution Assist
Stuck? Get instant AI hints or break down the optimal solution.
Stuck? The javascript course covers everything this challenge needs.