-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimpleCalculator.js
39 lines (28 loc) · 934 Bytes
/
simpleCalculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Javascript function to build a simple calculator app,
//1. Declare the function and the parameters
//2. write the logic in the code block using if,else, else if statements
//3. Use Console. log to call the function with dynamic parameters
//Declare function for simpleCalculator
function simpleCalculator (num1, num2, operation) {
//write the application logic.
let result ;
if (operation === "add")
{result = num1 + num2; }
else if (operation === "Subtract")
{result = num1 - num2; }
else if (operation === "multiply")
{result = num1 * num2; }
else if (operation === "divide")
//Check if num2 is not 0, cos you cant divide any number by zero. pro knowledge
{if (num2 !== 0 ) {
result = num1 / num2;
} else {
return "Error: Division by Zero ";
}
}
else {
return "Error: Invalid Operation";
}
return result;
};
console.log(simpleCalculator (20, 5, "divide"));