-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
59 lines (52 loc) · 2.02 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
document.addEventListener("DOMContentLoaded", () => {
var screen = document.querySelector(".screen");
var numkey = document.querySelectorAll(".numkey");
var binary_operator = document.querySelectorAll(".binarykey");
var result = document.querySelector("#result");
var del = document.querySelector("#del");
var clear = document.querySelector("#clear");
var pie = document.querySelector("#pi");
var percentage = document.querySelector("#percentage");
var lastdigit;
clear.onclick = () => {
screen.innerHTML = "";
screen.dataset.state = "begin";
}
del.onclick = () => {
screen.innerHTML = screen.innerHTML.slice(0,screen.innerHTML.length-1);
}
numkey.forEach((numkey) => {
numkey.onclick = function() {
lastdigit = screen.innerHTML[screen.innerHTML.length - 1];
if (screen.dataset.state === "cont") {
screen.innerHTML = this.innerHTML;
screen.dataset.state = "begin";
}
else screen.innerHTML += this.innerHTML;
}
})
binary_operator.forEach((binary_operator) => {
binary_operator.onclick = function() {
screen.dataset.state = "begin";
lastdigit = screen.innerHTML[screen.innerHTML.length - 1];
if (isNaN(parseInt(lastdigit))) {
alert('Invalid Entry');
}
else screen.innerHTML += this.innerHTML;
}
})
pie.onclick = function() {
lastdigit = screen.innerHTML[screen.innerHTML.length - 1];
if (!(isNaN(parseInt(lastdigit)))) screen.innerHTML += '*' + Math.PI;
else screen.innerHTML += Math.PI;
}
percentage.onclick = function() {
lastdigit = screen.innerHTML[screen.innerHTML.length - 1];
if (!(isNaN(parseInt(lastdigit)))) screen.innerHTML += '*0.01';
else screen.innerHTML += '0.01';
}
result.onclick = () => {
screen.innerHTML = eval(screen.innerHTML).toFixed(5);
screen.dataset.state = "cont";
}
});