-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
222 lines (188 loc) · 5.93 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"use strict";
//Buttons
const powerBtn = document.querySelector(".calc__power");
const soundBtn = document.querySelector(".calc__sound");
const clearBtn = document.querySelector(".clear");
const delBtn = document.querySelector(".del");
const resultBtn = document.querySelector(".result");
const btnElement = document.querySelectorAll(".btn");
const soundIcon = document.querySelector(".sound-icon");
//Screens
const calcScreen = document.querySelector(".calc__screen");
const outputScreen = document.querySelector(".calc__output");
const inputScreen = document.querySelector(".calc__input");
//Sounds
const powerSound = new Audio("sounds/beep.wav");
const clickSound = new Audio("sounds/click.wav");
const errorSound = new Audio("sounds/error.wav");
//Functions
//to simulate a button click animation
const clickedButtonAnimation = (btnElement) => {
btnElement.classList.add("btn--clicked");
if (!btnElement.classList.contains("power-off")) {
clickSound.play();
}
setTimeout(() => {
btnElement.classList.remove("btn--clicked");
}, 200);
};
//to display the button value to the screen
const inputScreenDisplay = (clickedValue) => {
inputScreen.value += clickedValue;
};
const calculationAlgorithm = () => {
try {
//to calculate input and print on output screen
outputScreen.value = eval(inputScreen.value);
//to check for divide-by-0 errors and input errors while calculating input
if (
isNaN(outputScreen.value) ||
outputScreen.value === Infinity ||
outputScreen.value === -Infinity
) {
throw new Error("Cannot divide by zero");
}
} catch (error) {
//to print an error message to the output screen with an error sound
clickSound.pause();
errorSound.play();
outputScreen.value = "Wahala!";
calcScreen.classList.add("error-screen");
outputScreen.classList.add("error-screen");
setTimeout(() => {
calcScreen.classList.remove("error-screen");
outputScreen.classList.remove("error-screen");
}, 200);
}
};
// to clear the entire screen
const clearScreen = () => {
outputScreen.value = "";
inputScreen.value = "";
};
//to delete a single character from the screen
const deleteChar = () => {
let inputScreenArray = [];
inputScreenArray = inputScreen.value.split("");
inputScreenArray.pop();
inputScreen.value = inputScreenArray.join("");
};
//to toggle sound
const soundLogic = () => {
const mute = (value) => {
powerSound.muted = value;
errorSound.muted = value;
clickSound.muted = value;
};
if (soundIcon.src.includes("volume-1")) {
mute(true);
soundBtn.classList.add("mute");
soundIcon.src = "icons/volume-2.svg";
} else {
mute(false);
soundBtn.classList.remove("mute");
soundIcon.src = "icons/volume-1.svg";
}
};
//to initiate an off state
const offState = () => {
clearScreen();
inputScreen.setAttribute("placeholder", "");
calcScreen.classList.add("inactive");
inputScreen.classList.add("inactive");
outputScreen.classList.add("inactive");
for (const item of btnElement) {
item.classList.add("power-off");
}
powerBtn.classList.remove("power-off");
soundBtn.classList.remove("power-off");
};
offState();
// Power button click event
powerBtn.addEventListener("click", function () {
if (powerBtn && !this.classList.contains("power-on-clr")) {
//to simulate a power on sequence
for (const item of btnElement) {
setTimeout(() => {
item.classList.remove("power-off");
}, 500);
setTimeout(() => {
inputScreen.setAttribute("placeholder", "0");
calcScreen.classList.remove("inactive");
inputScreen.classList.remove("inactive");
outputScreen.classList.remove("inactive");
powerSound.play();
}, 1000);
}
this.classList.add("power-on-clr");
} else {
//to initiate a power off sequence
setTimeout(() => {
offState();
}, 500);
this.classList.remove("power-on-clr");
}
});
//to loop over all the Button elements and add button animation to clicked buttons
for (const item of btnElement) {
item.addEventListener("click", function () {
clickedButtonAnimation(this);
//to ensure that only numbers and operators are printed to the input screen
if (this.value && !this.classList.contains("power-off")) {
inputScreenDisplay(this.value);
}
//to print calculated result to the output
//only when the calculator is on and the result button is clicked
if (
this.classList.contains("result") &&
!this.classList.contains("power-off")
) {
calculationAlgorithm();
}
//clears both screens when "C" button is clicked
if (this.classList.contains("clear")) {
clearScreen();
}
//removes one character from the screen when "DEL" button is clicked
if (this.classList.contains("del")) {
deleteChar();
}
//toggles sound when sound button is clicked
if (this.classList.contains("calc__sound")) {
soundLogic();
}
});
}
//Keyboard press events
document.addEventListener("keypress", function (event) {
const keyPressed = event.key;
// to display the button value on key press
for (const item of btnElement) {
if (keyPressed === item.value) {
if (item.value && !item.classList.contains("power-off")) {
inputScreenDisplay(item.value);
}
clickedButtonAnimation(item);
}
}
//to calculate input and print result on key press
if (keyPressed === "Enter" || keyPressed === "=") {
calculationAlgorithm();
clickedButtonAnimation(resultBtn);
}
//to delete a character from the screen on key press
if (keyPressed === "d" || keyPressed === "D") {
deleteChar();
clickedButtonAnimation(delBtn);
}
// to clear the screen on keypress
if (keyPressed === "c" || keyPressed === "C") {
clearScreen();
clickedButtonAnimation(clearBtn);
}
// to toggle sound on key press
if (keyPressed === "m" || keyPressed === "M") {
soundLogic();
clickedButtonAnimation(soundBtn);
}
});