
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Prefix calculator using stack in JavaScript
We are required to make a calculator with the RPN (reverse polish notation) input method using Stacks in JavaScript.
Consider the following input array −
const arr = [1, 5, '+', 6, 3, '-', '/', 7, '*'];
Process −
1 is an operand, push to Stack.
5 is an operand, push to Stack.
'+' is an operator, pop 1 and 5, calculate them and push result to Stack.
6 is an operand, push to Stack.
3 is an operand, push to Stack.
'−' is an operator, pop 6 and 3, subtract them and push result to Stack.
'/' is an operator, pop 6 and 3, divided them and push result to Stack.
7 is an operand, push to Stack.
'*' is an operator, pop 2 and 7, multiply them and push result to Stack.
And finally, the output should be −
const output = 14;
Example
The code for this will be −
const arr = [1, 5, '+', 6, 3, '−', '/', 7, '*']; const stackCalculator = (arr = []) => { const options = { '+': (a, b) => a + b, '−': (a, b) => a - b, '*': (a, b) => a * b, '/': (a, b) => a / b }; const stack = []; arr.forEach(value => { stack.push(value in options ? options[value](...stack.splice(-2)) : value ); }); return stack; }; console.log(stackCalculator(arr));
Output
And the output in the console will be −
[14]
- Related Articles
- Real Time Calculator using HTML, CSS, and JavaScript
- Switch case calculator in JavaScript
- Sorting elements of stack using JavaScript
- Creating a Simple Calculator using HTML, CSS, and JavaScript
- Create a Calculator function in JavaScript
- How to Create a Binary Calculator using HTML, CSS and JavaScript?
- Basic calculator program using Java
- Basic calculator program using Python
- Basic calculator program using C#
- Average Speed Calculator using Tkinter
- Ratio Calculator GUI using Tkinter
- Simple GUI calculator using Tkinter in Python
- How to design a Loan EMI Calculator using HTML, CSS and JavaScript?
- Not able to push all elements of a stack into another stack using for loop in JavaScript?
- Stack Data Structure in Javascript

Advertisements