
- 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
Create a Calculator function in JavaScript
We have to write a function, say calculator() that takes in one of the four characters (+, - , *, / ) as the first argument and any number of Number literals after that. Our job is to perform the operation specified as the first argument over those numbers and return the result.
If the operation is multiplication or addition, we are required to perform the same operation with every element. But if the operation is subtraction or division, we have to consider the first element as neutral and subtract all other elements from it or divide it by all other elements, based on the operation.
Therefore, let’s write the code for this function −
Example
const calculator = (operation, ...numbers) => { const legend = '+-*/'; const ind = legend.indexOf(operation); return numbers.reduce((acc, val) => { switch(operation){ case '+': return acc+val; case '-': return acc-val; case '*': return acc*val; case '/': return acc/val; }; }); }; console.log(calculator('+', 12, 45, 21, 12, 6)); console.log(calculator('-', 89, 45, 21, 12, 6)); console.log(calculator('*', 12, 45, 21, 12, 6)); console.log(calculator('/', 189000, 45, 7, 12, 4));
Output
The output in the console will be −
96 5 816480 12.5
- Related Articles
- How to Create a Binary Calculator using HTML, CSS and JavaScript?
- Java Program to create a Calculator
- Create a simple calculator using Java Swing
- How to Create a Profit and Loss Calculator using HTML, CSS, and JavaScript?
- Switch case calculator in JavaScript
- Create a custom toLowerCase() function in JavaScript
- Program to create grade calculator in Python
- Prefix calculator using stack in JavaScript
- Python Program to Create a class performing Calculator Operations
- How to create a function from a string in JavaScript?
- Creating a Simple Calculator using HTML, CSS, and JavaScript
- Function to create diamond shape given a value in JavaScript?
- How to create a function that invokes each provided function using JavaScript?
- Golang Program to create a Class that can perform basic Calculator Operations
- How to Design a Calculator with Neumorphism Effect/Soft UI in JavaScript?

Advertisements