By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.
Reverse Polish Notation Calculator
YandexAvito
Write a function calc(str) that evaluates an expression in Reverse Polish Notation (RPN).
RPN is an expression where operands and operators are written in a special order:
- Operands (numbers) and operators (+, -, *, /) are separated by spaces.
- An operation is performed on the two most recent numbers encountered before the operator.
- The expression is evaluated from left to right.
If the input string is incorrect, the function should return an error message.
Possible errors:
"Error in Syntax" — if the expression contains a syntax error.
"Error in Operands" — if an operand is not a number.
"Division by zero" — if division by zero occurs in the expression.
Examples:
Input 1:"8 2 / 3 +"
Output 1:7
Input 2:"7 2 3 * -"
Output 2:1
JavaScript•UTF-8
Run your code to see results.
Click the Run button above
Reverse Polish Notation Calculator
YandexAvito
Write a function calc(str) that evaluates an expression in Reverse Polish Notation (RPN).
RPN is an expression where operands and operators are written in a special order:
- Operands (numbers) and operators (+, -, *, /) are separated by spaces.
- An operation is performed on the two most recent numbers encountered before the operator.
- The expression is evaluated from left to right.
If the input string is incorrect, the function should return an error message.
Possible errors:
"Error in Syntax" — if the expression contains a syntax error.
"Error in Operands" — if an operand is not a number.
"Division by zero" — if division by zero occurs in the expression.