AniUI Academy
easy+150 XPPractice

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

  • push returns the size of the stack after the value was added.
  • pop returns values in last-in-first-out order.
  • pop and peek return undefined on an empty stack rather than throwing.
  • peek leaves the stack unchanged.
  • isEmpty() is true only when size() is 0.

Your workspace

Try it yourself
Loading playground...

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.