-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
46 lines (38 loc) · 1.09 KB
/
app.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
let screen=document.querySelector('input');
let button=document.querySelectorAll('button');
for(item of button){
item.addEventListener('click',(event)=>{
if(event.target.innerText=='C') screen.value="";
else if(event.target.innerText=='='){
try{
screen.value=eval(screen.value);
}
catch(err){
screen.value="Invalid";
}
}
else{
screen.value+=(event.target.innerText);
}
})
}
screen.addEventListener('keydown',function(e){
e.preventDefault();
})
document.addEventListener('keydown',(event)=>{
if(event.key=="Enter"){
try{
screen.value=eval(screen.value);
}
catch(err){
screen.value="Invalid";
}
}
else if(event.key=="Backspace") screen.value=screen.value.substring(0,screen.value.length-1);
else screen.value+=isValid(event.key);
})
function isValid(e){
let arr=['1','2','3','4','5','6','7','8','9','0','*',"%",'/','-','+'];
for(item of arr) if(e==item) return item;
return "";
}