-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
107 lines (101 loc) · 3.09 KB
/
calculator.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
101
102
103
104
105
106
107
let answerDisplay = document.querySelector(".topSection");
let answer = document.querySelector(".answerSection");
let resultDisplayed = false;
// Displaying numbers
let numbers = document.querySelectorAll(".number");
numbers.forEach((number) => {
number.addEventListener("click", () => {
if(resultDisplayed) {
answer.innerText += number.innerText;
}
else {
answerDisplay.innerText += number.innerText;
}
});
});
// Displaing operator
let otherOptions = document.querySelectorAll(".otherOptions");
otherOptions.forEach((option) => {
option.addEventListener("click", () => {
if(resultDisplayed) {
answer.innerText += option.innerText;
}
else {
answerDisplay.innerText += option.innerText;
}
});
});
// Delete function
let deleteOne = document.querySelector(".deleteOne");
deleteOne.addEventListener("click", () => {
if(resultDisplayed) {
answer.innerText = answer.innerText.slice(0, -1);
}
else {
answerDisplay.innerText = answerDisplay.innerText.slice(0, -1);
}
});
// Clear function
let clearAll = document.querySelector(".clearAll");
clearAll.addEventListener("click", () => {
resultDisplayed = false;
answerDisplay.innerText = "";
answer.innerText = "";
answer.style.display = "none";
answerDisplay.classList.remove("changes");
});
// Result function
let equal = document.querySelector(".equal")
equal.addEventListener("click", () => {
if(resultDisplayed) {
answerDisplay.innerText = answer.innerText;
}
answer.style.display = "block";
answer.innerText = parseFloat(eval(answerDisplay.innerText));
answerDisplay.classList.add("changes");
resultDisplayed = true;
});
// Displaying numbers through keyboard
let operators = ["+", "-", "*", "/", "%", "."];
document.addEventListener("keydown", (e) => {
if(!(isNaN(e.key))) {
if(resultDisplayed) {
answer.innerText += e.key;
}
else {
answerDisplay.innerText += e.key;
}
}
if(e.keyCode == 8) {
if(resultDisplayed) {
answer.innerText = answer.innerText.slice(0, -1);
}
else {
answerDisplay.innerText = answerDisplay.innerText.slice(0, -1);
}
}
if(operators.includes(e.key)) {
if(resultDisplayed) {
answer.innerText += e.key;
}
else {
answerDisplay.innerText += e.key;
}
}
if(e.keyCode == 13) {
if(resultDisplayed) {
answerDisplay.innerText = answer.innerText;
}
answer.style.display = "block";
answer.innerText = eval(answerDisplay.innerText);
answerDisplay.classList.add("changes");
resultDisplayed = true;
}
if(e.keyCode == 27) {
resultDisplayed = false;
answerDisplay.innerText = "";
answer.innerText = "";
answer.style.display = "none";
answerDisplay.classList.remove("changes");
}
});