-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculator.js
46 lines (44 loc) · 1.5 KB
/
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
43
44
45
46
var button = document.getElementsByClassName("button");
var display = document.getElementById("resultBox");
var operand1 = 0;
var operand2 = null;
var operator = null;
for(var i=0; i<button.length; i++){
button[i].addEventListener('click',function(){
var value = this.getAttribute("data-value");
if(value=="AC"){
display.innerText = "";
}else if(value == '+'){
operator = '+';
operand1 = parseFloat(display.textContent);
display.innerText = "";
} else if(value == '-'){
operator = '-';
operand1 = parseFloat(display.textContent);
display.innerText = "";
} else if(value == '*'){
operator = '*';
operand1 = parseFloat(display.textContent);
display.innerText = "";
} else if(value == '/'){
operator = '/';
operand1 = parseFloat(display.textContent);
display.innerText = "";
} else if(value == '%'){
operator = '%';
operand1 = parseFloat(display.textContent);
display.innerText = "";
}else if(value == '='){
operand2 = parseFloat(display.textContent);
display.innerText = "";
if(operand1 != 0 && operand2 == 0){
display.innerText = "Zero Division Is Not Possible";
return;
}
var ans = eval(operand1 + operator + operand2);
display.innerText = ans;
}else{
display.innerText += value;
}
});
}