-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (61 loc) · 1.78 KB
/
index.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
const form = document.getElementById("calc-form");
form.addEventListener("submit", (e) => e.preventDefault());
const output = document.getElementById("output");
const operands = document.querySelectorAll("button[data-type=operand]");
const operators = document.querySelectorAll("button[data-type=operator]");
const clearBtn = document.querySelector("button[data-type=clear]");
let isOperator = false;
const removeActive = () => {
operators.forEach((btn) => btn.classList.remove("active"));
};
operands.forEach((btn) => {
btn.addEventListener("click", (e) => {
removeActive();
if (output.value == "0" || isOperator) {
isOperator = false;
output.value = e.target.value;
} else if (output.value.includes(".")) {
output.value += e.target.value.replace(".", "");
} else {
output.value += e.target.value;
}
clearBtn.textContent = "C";
});
});
let equation = [];
operators.forEach((btn) => {
btn.addEventListener("click", (e) => {
removeActive();
e.currentTarget.classList.add("active");
switch (e.target.value) {
case "%":
output.value /= 100;
break;
case "invert":
output.value *= -1;
break;
case "=":
equation.push(output.value);
output.value = eval(equation.join(""));
equation = [];
break;
default:
let lastItem = equation[equation.length - 1];
if (["/", "*", "+", "-"].includes(lastItem) && isOperator) {
equation.pop();
}
equation.push(output.value, e.target.value);
isOperator = true;
break;
}
});
});
clearBtn.addEventListener("click", (e) => {
output.value = "0";
if (clearBtn.textContent === "C") {
clearBtn.textContent = "AC";
} else {
removeActive();
equation = [];
}
});