w3resource

JavaScript: Reverse Polish notation in mathematical expression

JavaScript Math: Exercise-70 with Solution

Write a JavaScript program to apply Reverse Polish notation in 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:

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript function to reverse Polish notation in mathematical expression</title>
</head>
<body>

</body>
</html>

JavaScript Code:

const RPN_calculation = (math_expr) => {
  const operators = {
    '+': (x, y) => x + y,
    '-': (x, y) => x - y,
    '*': (x, y) => x * y,
    '/': (x, y) => y / x
  }
  const expr_part = math_expr.split(' ')
  const data = []

  expr_part.forEach((expr_part) => {
    const operator = operators[expr_part]
    if (typeof operator === 'function') 
    {
      const x = data.pop()
      const y = data.pop()
      const result = operator(x, y)
      data.push(result)
    } 
    else 
    {
      data.push(parseFloat(expr_part))
    }
  })
  return data.pop()
}
console.log(RPN_calculation("5 8 *"))
console.log(RPN_calculation("5 6 + 2 *"))
console.log(RPN_calculation("5 3 4 * +"))

Sample Output:

40
22
17

Flowchart:

JavaScript Math flowchart of reverse Polish notation in mathematical expression

Live Demo:

See the Pen javascript-math-exercise-70 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus

Previous: Find all prime numbers below a given number.
Next: Javascript Array Exercises

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

JavaScript: Tips of the Day

function and arguments

const person = {
  name: 'Lydia Hallie',
  hobbies: ['coding'],
};

function addHobby(hobby, hobbies = person.hobbies) {
  hobbies.push(hobby);
  return hobbies;
}

addHobby('running', []);
addHobby('dancing');
addHobby('baking', person.hobbies);

console.log(person.hobbies);

The addHobby function receives two arguments, hobby and hobbies with the default value of the hobbies array on the person object.
First, we invoke the addHobby function, and pass "running" as the value for hobby and an empty array as the value for hobbies. Since we pass an empty array as the value for y, "running" gets added to this empty array.
Then, we invoke the addHobby function, and pass "dancing" as the value for hobby. We didn't pass a value for hobbies, so it gets the default value, the hobbies property on the person object. We push the hobby dancing to the person.hobbies array.
Last, we invoke the addHobby function, and pass "bdaking" as the value for hobby, and the person.hobbies array as the value for hobbies. We push the hobby baking to the person.hobbies array.
After pushing dancing and baking, the value of person.hobbies is ["coding", "dancing", "baking"]

Ref: https://bit.ly/2Hcpkm6