1. Operator table
Map each operator to a function; truncate division toward zero.
"/": (a, b) => Math.trunc(a / b)Typorithm · medium
Step 1 of 8 · intro card
Stacks & Queues
Push numbers; on an operator, pop two operands (right first), apply, and push the result.
Reverse Polish Notation places operators after their operands, so no parentheses are needed.
A stack holds operands; an operator pops the top two and pushes the result.
Order matters: the top of the stack is the right operand, popped first.
Numbers push directly.
Operators pop two (right first).
Push the computed result back.
function evalRPN(tokens) {
const stack = [];
const ops = {
"+": (a, b) => a + b,
"-": (a, b) => a - b,
"*": (a, b) => a * b,
"/": (a, b) => Math.trunc(a / b),
};
for (const token of tokens) {
if (token in ops) {
const b = stack.pop();
const a = stack.pop();
stack.push(ops[token](a, b));
} else {
stack.push(Number(token));
}
}
return stack.pop();
}Connect each part of the code to the concept before you type.
Map each operator to a function; truncate division toward zero.
"/": (a, b) => Math.trunc(a / b)The right operand is on top, so it pops first.
const b = stack.pop()The left operand pops second.
const a = stack.pop()The computed value becomes a new operand.
stack.push(ops[token](a, b))Numbers go straight onto the stack.
stack.push(Number(token))Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.