-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
58 lines (55 loc) · 1.87 KB
/
script.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
47
48
49
50
51
52
53
54
55
56
57
58
let display = document.getElementById('display');
let buttons = Array.from(document.getElementsByClassName('button'));
buttons.map( button => {
button.addEventListener('click', (e) => {
switch(e.target.innerText){
case 'C':
display.innerText = '';
break;
case '←':
if(display.innerText){
display.innerText = display.innerText.slice(0,-1);
}
break;
case '=':
try{
display.innerText = eval(display.innerText);
}catch{
display.innerText = 'Error';
}
break;
default:
display.innerText+=e.target.innerText;
}
})
})
document.addEventListener('keyup', (event) => {
var name = event.key;
switch(name){
case 'C':
display.innerText = '';
break;
case 'Backspace':
if(display.innerText){
display.innerText = display.innerText.slice(0,-1);
}
break;
case '=':
try{
display.innerText = eval(display.innerText);
}catch{
display.innerText = 'Error';
}
break;
case 'Enter':
try{
display.innerText = eval(display.innerText);
}catch{
display.innerText = 'Error';
}
break;
default:
if(name == '1' || name == '2' || name == '3' || name == '4' || name == '5' || name == '6' || name == '7' || name == '8' || name == '9' || name == '+' || name == '-' || name == '*' || name == '/' || name == ')' || name == '(' || name == '0')
display.innerText+=name;
}
}, false);