-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
38 lines (38 loc) · 1.06 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
var buttons=document.getElementsByClassName("button");
console.log(typeof buttons);
var display=document.getElementById("display");
var operation=null;
var operand1=0;
var operand2=null;
var task='+';
for(var i=0;i<buttons.length;i++){
buttons[i].addEventListener("click",function(){
var value=this.getAttribute("data-value");
if(value=="+/-")
{
task='-';
display.innerText=task;
}
if(value=='+' || value=='-' || value=='*' || value=='/' || value=='%'){
operation=value;
operand1=parseFloat(display.textContent);
display.innerText="";
task='+';
}
else if(value=='='){
operand2=parseFloat(sign+display.textContent);
var result=eval(operand1+""+operation+""+operand2);
display.innerText=result;
}
else if(value=="ac"){
display.innerText="";
}
else
{
if(value!="+/-")
{
display.innerText+=value;
}
}
});
}