-
Notifications
You must be signed in to change notification settings - Fork 0
/
20. Make_a_simple_calculator.js
43 lines (38 loc) · 1.09 KB
/
20. Make_a_simple_calculator.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
35
36
37
38
39
40
41
42
//* ------- This program implements a simple calculator that can perform basic arithmetic operations. It prompts the user to enter two numbers and choose an operation(add,subtract,multiply,divison). It then perform the selected operation and displays the result.
let result;
function calculator(num1, num2, operation) {
if (!isNaN(num1) && !isNaN(num2)) {
switch (operation) {
case "+":
result = num1 + num2;
console.log(num1 + num2);
break;
case "-":
result = num1 - num2;
console.log(num1 - num2);
break;
case "*":
result = num1 * num2;
console.log(num1 * num2);
break;
case "/":
if (num2 != 0) {
result = num1 / num2;
console.log(num1 / num2);
} else {
console.log("Error! Division by zero is not allowed.");
break;
}
break;
default:
console.log("Invalid operation");
break;
}
}
}
calculator(3, 6, "+");
if (result !== undefined) {
console.log(result);
} else {
console.log("Error! Invalid input");
}