-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.js
More file actions
40 lines (38 loc) · 1.09 KB
/
Copy pathCalculator.js
File metadata and controls
40 lines (38 loc) · 1.09 KB
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
const display_calculation = document.querySelector(".displaychild1");
const display_result = document.querySelector(".displaychild2");
const buttons = document.querySelectorAll("button");
const specialChars = [".", "*", "/", "-", "+", "="];
let output = "";
let checkfocus = true;
const calculate = (BtnValue) => {
if (BtnValue === "=" && output !== "") {
checkfocus = false;
output = eval(output);
if (checkfocus == false)
{
display_calculation.value = "";
}
display_result.value = output;
}
else if (BtnValue === "CE" || checkfocus == false) {
output = "";
checkfocus = true;
display_result.value = "";
} else if (BtnValue === "Del") {
output = output.toString().slice(0, -1);
} else {
if (output === "" && specialChars.includes(BtnValue)) return;
output += BtnValue;
}
if (checkfocus)
{
display_calculation.value = output;
}
}
buttons.forEach((button) => {
button.addEventListener("click", (e) =>
{
calculate(e.target.dataset.value);
}
);
});