The Stack Protocol (LIFO)

EasyAcc. 98.5%
+15 XP 5

The Undo Mechanism (LIFO)

A Stack is a fundamental data structure modeled after a real-world stack of plates. It follows the LIFO (Last-In, First-Out) principle: the last item you place on the stack is the very first one you take off. This is exactly how the "Undo" button in your text editor works!

The Assignment

Your mission is to simulate a basic stack processor. Your function receives an array of commands and an array of values.

  1. Initialize an empty array to serve as your internal stack.
  2. Iterate through the commands array:
    • If the command is "PUSH", add the corresponding value from values[i] to the top of the stack.
    • If the command is "POP", remove the top element and print it.
    • If the command is "PEEK", print the top element without removing it.

01EXAMPLE 1

Inputcommands=["PUSH", "PUSH", "POP"], values=[10, 20, 0]
Output20

Explanation: 20 was pushed last, so it pops first.

Constraints

  • Use standard array methods (`push`, `pop`).
Data StructuresStack
JavaScript
Loading...
1 Hidden

Input Arguments

commands["PUSH","PUSH","POP"]
values[10,20,0]

Expected Output

20

Click RUN to test your solution