-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
143 lines (111 loc) · 4.42 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
// --------------------- Global variables ------------------------
const wrapper = document.querySelector(".wrapper");
const body = document.body;
const darkMode = document.querySelector(".toggleDark-mode");
const button = document.querySelectorAll(".button");
const resetButton = document.querySelector(".reset-button");
const deleteButton = document.querySelector(".delete-button");
const modulusButton = document.querySelector(".modulus-button");
const equalButton = document.querySelector(".equal-button");
const numberButton = document.querySelectorAll(".number");
const operatorButton = document.querySelectorAll(".operator");
const decimalButton = document.querySelector(".decimal-button");
// --------------------- build the calculator ------------------------
let operator = "";
let previousValue = "";
let currentValue = "";
let numberDisplay = document.querySelector(".number-display");
numberDisplay.textContent = "0";
let totalDisplay = document.querySelector(".total-display");
totalDisplay.textContent = "0";
numberButton.forEach((number) => number.addEventListener("click", function (e) {
handleNumber(e.target.textContent);
numberDisplay.textContent = previousValue + operator + currentValue;
}));
function handleNumber(number) {
if (currentValue.length < 6) {
currentValue += number;
}
}
operatorButton.forEach((op) => op.addEventListener("click", function (e) {
handleOperator(e.target.textContent);
numberDisplay.textContent = previousValue + operator;
}));
function handleOperator(op) {
operator = op;
if (previousValue !== "") {
operate();
}
previousValue = currentValue;
currentValue = "";
}
function operate() {
switch (operator) {
case "+":
totalDisplay.textContent = Math.round((parseInt(previousValue) + parseInt(currentValue)) * 100 + Number.EPSILON) / 100;
break;
case "-":
totalDisplay.textContent = Math.round((parseInt(previousValue) - parseInt(currentValue)) * 100 + Number.EPSILON) / 100;
break;
case "*":
totalDisplay.textContent = Math.round((parseInt(previousValue) * parseInt(currentValue)) * 100 + Number.EPSILON) / 100;
break;
case "/":
if (currentValue == "0") {
totalDisplay.textContent = "err..";
totalDisplay.style.color = "#DC3535";
}
else {
totalDisplay.textContent = Math.round((parseInt(previousValue) / parseInt(currentValue)) * 100 + Number.EPSILON) / 100;
}
break;
default:
console.log("default");
break;
}
}
equalButton.addEventListener("click", function () {
operate();
previousValue = "";
currentValue = totalDisplay.textContent;
});
function deleteLastChar() {
if (currentValue.length > 0) {
currentValue = currentValue.slice(0, -1);
numberDisplay.textContent = currentValue;
}
}
deleteButton.addEventListener("click", deleteLastChar);
// --------------------- Style and dark-mode ------------------------
numberButton.forEach(numberButton => {
numberButton.style.color = "#990000";
});
darkMode.addEventListener("click", function () {
const nightText = document.querySelector(".bi");
const logos = document.querySelectorAll(".logo"); // Select all elements with class 'logo'
if (this.classList.toggle("bi-lightbulb")) {
body.classList.add('dark-mode');
wrapper.classList.add('wrapper-dark-mode');
logos.forEach(function (logo) { // Loop through each logo element
logo.style.filter = 'invert(100%)';
});
} else {
body.classList.remove('dark-mode');
wrapper.classList.remove('wrapper-dark-mode');
logos.forEach(function (logo) { // Loop through each logo element
logo.style.filter = 'invert(0%)';
});
}
});
//-------------- Reset the calculator ----------------
resetButton.addEventListener("click", function () {
const strConfirm = confirm("Are you sure you want to delete everything?");
if (strConfirm == true) {
operator = "";
previousValue = "";
currentValue = "";
numberDisplay.textContent = "0";
totalDisplay.textContent = "0";
totalDisplay.style.color = "#191a19";
}
})