This content originally appeared on DEV Community and was authored by Shubham Jadhav
How to create calculator function in JavaScript??
Yes, Today we are making a JavaScript function which is return some mathematics output.
So, let's start with JavaScript code.
(1) Let's make a JavaScript
file and give any name
here I am giving index.js.
(Note:- We must need a
html file to run our JS
code in browser)
(2) Next step make a function
name calculator() in js
file. And pass three
parameter x, y and o.
(3) Parameters explanation :-
(a) x is a first number
(b) y is a second number
(c) o is a operator
(4) We are using switch
statement as shown in
following code.
(4) Let's understand above
code :-
(a) first pass o
parameter because it
will decide which
mathematics operation
will conduct. o is
String and x and y
number
(b) If any user call this
function by passing three
parameters like
calculator(2,3,'+') then
the first case is
executed.
(c) When anyone not enter
valid input the it will
return "enter valid
numbers".
(5) Let's call the above
function and see output.
? Happy Coding...
? Thank you for reading...
This content originally appeared on DEV Community and was authored by Shubham Jadhav
Shubham Jadhav | Sciencx (2021-09-18T05:56:30+00:00) Calculator Function using JavaScript ??. Retrieved from https://www.scien.cx/2021/09/18/calculator-function-using-javascript-%f0%9f%9a%80%f0%9f%9a%80/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.