-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.js
More file actions
60 lines (59 loc) · 1.83 KB
/
calculator.js
File metadata and controls
60 lines (59 loc) · 1.83 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// simple calculator
NoOfTimes = 0
while (NoOfTimes != 5){
const num1 = parseFloat(prompt("Enter first number: "))
const num2 = parseFloat(prompt('Enter second number: '))
console.log(`
Choose an operation
1. Enter 1 for Addition
2. Enter 2 for Subtraction
3. Enter 3 for Multiplication
4. Enter 4 for Divison
`)
const sign = prompt("Enter the number corresponding to the operation: ")
if (sign == 1){
if (isNaN(num1) || isNaN(num2)){
console.log("Number is not integer")
NoOfTimes += 1
}else{
result = num1 + num2
console.log(`${num1 } + ${num2} = ${result}`)
break
}
}else if (sign == 2){
if (isNaN(num1) || isNaN(num2)){
console.log("Number is not integer")
NoOfTimes += 1
}else{
result = num1 - num2
console.log(`${num1 } - ${num2} = ${result}`)
break
}
}else if (sign == 3){
if (isNaN(num1) || isNaN(num2)){
console.log("Number is not integer")
NoOfTimes += 1
}else{
result = num1 * num2
console.log(`${num1 } * ${num2} = ${result}`)
break
}
}else if (sign == 4){
if (isNaN(num1) || isNaN(num2)){
console.log("Number is not integer")
NoOfTimes += 1
}else{
if (num2 == 0){
console.log("Math operation cannot be carried out")
NoOfTimes = NoOfTimes +1
}else{
result = num1 / num2
console.log(`${num1 } / ${num2} = ${result}`)
break
}
}
}else{
console.log("Invalid operation")
NoOfTimes = NoOfTimes +1
}
}