forked from COPS-CSOC-2022/CSOC22-Week1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
88 lines (82 loc) · 2.78 KB
/
main.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
let display = document.getElementById('display');
let buttons = Array.from(document.getElementsByClassName('button'));
buttons.map(button => {
button.addEventListener('click', (e) => {
switch (e.target.innerText) {
// Here we added break after end of case C because it was leaving C
// we stopped its execution after printing'' so it left default case
case 'C':
display.innerText = '';
break;
case '←':
display.innerText = display.innerText.slice(0, -1);
break;
// here eval is used to evaluate mathematical operations
case '=':
try {
display.innerText = eval(display.innerText);
break;
}
catch {
display.innerText = 'Error!';
break;
}
case 'sin':
{
var x = display.innerText;
x = x * Math.PI / 180;
display.innerText = Math.sin(x);
break;
}
case 'cos':
{
var x = display.innerText;
x = x * Math.PI / 180;
display.innerText = Math.cos(x);
break;
}
case 'tan':
{
var x = display.innerText;
x = x * Math.PI / 180;
display.innerText = Math.tan(x);
break;
}
case '√':
{
var x = display.innerText;
display.innerText = Math.sqrt(x);
break;
}
case 'cosec':
{
var x = display.innerText;
x = x * Math.PI / 180;
display.innerText = 1 / Math.sin(x);
break;
}
case 'cot':
{
var x = display.innerText;
x = x * Math.PI / 180;
display.innerText = 1 / Math.tan(x);
break;
}
case 'sec':
{
var x = display.innerText;
x = x * Math.PI / 180;
display.innerText = 1 / Math.cos(x);
break;
}
case 'ln':
{
var x = display.innerText;
display.innerText = Math.log(x);
break;
}
default:
display.innerText += e.target.innerText;
}
});
});