JavaScript: Reverse Polish notation in mathematical expression
JavaScript Math: Exercise-70 with Solution
Reverse Polish Notation (RPN)
Write a JavaScript program to apply reverse Polish notation to a given mathematical expression.
From Wikipedia -
Reverse Polish notation (RPN), also known as reverse Łukasiewicz notation, Polish postfix notation or simply postfix notation, is a mathematical notation in which operators follow their operands, in contrast to Polish notation (PN), in which operators precede their operands. It does not need any parentheses as long as each operator has a fixed number of operands. The description "Polish" refers to the nationality of logician Jan Łukasiewicz, who invented Polish notation in 1924.
Sample Data:
"5 8 *" -> 40
"5 6 + 2 *" -> 22
"5 3 4 * +" -> 17
Sample Solution:
JavaScript Code:
Output:
40 22 17
Flowchart:

Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript function that evaluates a mathematical expression written in Reverse Polish Notation using a stack-based approach.
- Write a JavaScript function that converts an infix expression to Reverse Polish Notation and then evaluates it.
- Write a JavaScript function that handles error cases such as insufficient operands and invalid operators in an RPN expression.
- Write a JavaScript function that supports both integer and floating-point arithmetic when evaluating an RPN expression.
Go to:
PREV : Find All Primes Below N.
NEXT : Prime Factors of a Number.
Improve this sample solution and post your code through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.