Reverse Polish Calculator

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write evalRPN(tokens): tokens is an expression in Reverse Polish Notation, an array of strings where each operator comes after its two operands. A token is a whole number, such as "12" or "-3", or one of "+", "-", "*" and "/". Return the value as a number. Division truncates toward zero, so 7 divided by -2 is -3. The expression is always valid, never divides by zero, and stays within safe whole numbers. For example, evalRPN(["2", "1", "+", "3", "*"]) is 9, because 2 plus 1, times 3, is 9.

Your code must pass

  • Adds, subtracts, multiplies and divides
  • Keeps the operands in order
  • Truncates division toward zero
  • Handles a single number and negative numbers