-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
114 lines (104 loc) · 3.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
// read history value from UI
function getHistoryValue() {
return document.getElementById("history").innerText;
}
// write stuff to history area on UI
function printHistoryValue(num) {
document.getElementById("history").innerText = num;
}
// read output value from UI
function getOutputValue() {
return document.getElementById("output").innerText;
}
// write stuff to output area on UI
function printOutputValue(num) {
if (num === "") {
document.getElementById("output").innerText = num;
}
else {
document.getElementById("output").innerText = getFormattedNumber(num);
}
}
// nicely formats output string value to a comma separated value
function getFormattedNumber(num) {
if (num == "-") {
return "";
}
var n = Number(num);
var value = n.toLocaleString("en");
return value;
}
// remove comma separation format from formatted output
function reverseNumberFormat(num) {
return Number(num.replace(/,/g, ""));
}
addEventListener("DOMContentLoaded", function () {
// listen for operator keys click events
var operators = document.getElementsByClassName("op_key");
var len = operators.length;
for (i = 0; i < len; i++) {
operators[i].addEventListener("click", function () {
//AC button clicked
if (this.id == "clear") {
printHistoryValue("");
printOutputValue("");
}
//Del button clicked
else if (this.id == "backspace") {
var output = reverseNumberFormat(getOutputValue()).toString();
// check whether output has a value then remove last character and print to UI
if (output) {
output = output.substr(0, output.length - 1);
printOutputValue(output);
}
}
else {
var output = getOutputValue();
var history = getHistoryValue();
// truncate non-numeric type last character from history value
if (output === "" && history !== "") {
if (isNaN(history[history.length - 1])) {
history = history.substr(0, history.length - 1);
}
}
if (output !== "" || history !== "") {
// tenary operation to set output to empty when it is empty
output = output === "" ? output : reverseNumberFormat(output);
history += output;
if (this.id === "=") {
var result = eval(history);
printOutputValue(result);
printHistoryValue("");
}
else {
history += this.id;
printHistoryValue(history);
printOutputValue("");
}
}
}
})
}
// listen for number keys click events
var numbers = document.getElementsByClassName("num_key");
var val = numbers.length;
for (i = 0; i < val; i++) {
numbers[i].addEventListener("click", function () {
var output = reverseNumberFormat(getOutputValue());
if (!isNaN(output)) {
output += this.id;
printOutputValue(output);
}
})
}
});
//For Toogle Switch functioning
const toggleSwitch = document.querySelector('.toggle-switch');
const themeStyle = document.getElementById('theme-style');
const themeStyleDark = document.getElementById('theme-style-dark');
toggleSwitch.addEventListener('click', () => {
document.body.classList.toggle('dark-mode');
toggleSwitch.classList.toggle('dark');
themeStyle.disabled = !themeStyle.disabled;
themeStyleDark.disabled = !themeStyleDark.disabled;
});