-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathscript.js
100 lines (100 loc) · 2.58 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
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 '=':
try {
display.innerText = eval(display.innerText);
}
catch {
display.innerText = "Error"
}
break;
case 'CE':
if (display.innerText) {
display.innerText = display.innerText.slice(0, -1);
}
break;
case '%':
try {
display.innerText = display.innerText / 100;
}
catch {
display.innerText = "Error"
}
break;
case 'sqrt':
try {
display.innerText =(Math.sqrt(display.innerText));
break;
}
catch {
display.innerText = "Error"
}
break;
case 'sin':
try {
display.innerText = Math.sin(display.innerText);
break;
}
catch {
display.innerText = "Error"
}
break;
case 'cos':
try {
display.innerText = Math.cos(display.innerText);
break;
}
catch {
display.innerText = "Error"
}
break;
case 'tan':
try {
display.innerText = Math.tan(display.innerText);
break;
}
catch {
display.innerText = "Error"
}
break;
case 'pi':
display.innerText += 3.1415;
break;
//
default:
display.innerText += e.target.innerText;
}
});
});
document.addEventListener('keyup', (e) => {
if(((e.which >= 48 && e.which <= 57) || (e.which>= 96 && e.which <= 111) || (e.which >= 189 && e.which <= 191)) && !e.shiftKey){
display.innerText += e.key;
}
if(e.shiftKey && (e.which == 187 || e.which == 48 || e.which == 56 || e.which == 57 || e.which == 53)){
display.innerText += e.key;
}
if(e.which == 54 && e.shiftKey){
display.innerText += "**";
}
if(e.which == 13){
try {
display.innerText = eval(display.innerText);
}
catch {
display.innerText = "Error"
}
}
if(e.which == 8){
display.innerText = display.innerText.slice(0, -1);
}
if(e.which == 46){
display.innerText = "";
}
})