-
Notifications
You must be signed in to change notification settings - Fork 13
/
script.js
136 lines (123 loc) · 3.92 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
let calculationString = "";
let calculationHistory = []; // To store the calculation history
const maxHistoryItems = 8;
const input = document.querySelector(".input");
//dark theme functionality:
const icon = document.getElementById("icon");
icon.onclick = function () {
document.body.classList.toggle("dark-theme");
if (document.body.classList.contains("dark-theme")) {
icon.src = "images/sun.png";
} else {
icon.src = "images/moon.png";
}
};
// Function to update the input field and store history
function updateInputAndHistory() {
document.querySelector("input").value = calculationString;
localStorage.setItem(
"calculationHistory",
JSON.stringify(calculationHistory)
);
updateHistoryList(); // Update the history list
}
// Load calculation history from local storage if it exists
const savedCalculationHistory = localStorage.getItem("calculationHistory");
if (savedCalculationHistory) {
calculationHistory = JSON.parse(savedCalculationHistory);
updateHistoryList(); // Update the history list when the page loads
}
function updateHistoryList() {
const historyList = document.getElementById("history-list");
historyList.innerHTML = ""; // Clear the existing list
calculationHistory.forEach((calculation) => {
const listItem = document.createElement("li");
listItem.textContent = calculation;
historyList.appendChild(listItem);
});
}
let buttons = document.querySelectorAll(".button");
Array.from(buttons).forEach((button) => {
button.addEventListener("click", (e) => {
if (e.target.innerHTML == "=") {
calculate();
} else if (e.target.innerHTML == "C") {
calculationString = "";
updateInputAndHistory();
} else if (e.target.innerHTML == "←") {
calculationString = calculationString.slice(
0,
calculationString.length - 1
);
updateInputAndHistory();
} else {
calculationString += e.target.innerHTML;
updateInputAndHistory();
}
});
});
function calculate() {
calculationString = input.value;
try {
if (calculationString && calculationString.match(/[+\-*\/]/g)) {
let result = eval(calculationString);
calculationHistory.push(`${calculationString} = ${result}`);
calculationString = result.toString();
updateInputAndHistory();
}
} catch (e) {
input.value = "";
input.classList.add("shake"); // Add shake animation class
setTimeout(() => {
input.classList.remove("shake"); // Remove shake animation class after animation
}, 500);
}
}
input.addEventListener("keydown", function (e) {
if (!isValid(e.key)) {
input.classList.add("shake"); // Add shake animation class
setTimeout(() => {
input.classList.remove("shake"); // Remove shake animation class after animation
}, 500);
e.preventDefault();
}
if (e.key == "Enter") {
calculate();
}
});
function isValid(key) {
return (
!isNaN(parseFloat(key)) ||
["+", "-", "*", "/", "=", "Enter", "Backspace", "Shift", "."].includes(key)
);
}
/** clear Calculation History **/
const clearBtn = document.querySelector(".clearHistory");
clearBtn.addEventListener("click", function (e) {
localStorage.clear();
calculationHistory = [];
clearHistory("history-list");
});
function clearHistory(parentId) {
const parent = document.querySelector(`#${parentId}`);
while (parent.lastChild) {
parent.removeChild(parent.lastChild);
}
if (e.key == "Enter") {
calculate();
}
}
function isValid(key) {
return (
!isNaN(parseFloat(key)) ||
["+", "-", "*", "/", "=", "Enter", "Backspace", "Shift", "."].includes(key)
);
}
//Onclick Sound functionality:
const click_sound = document.querySelector("#click-sound");
buttons.forEach((button) => {
button.addEventListener("click", () => {
click_sound.currentTime = 0;
click_sound.play();
});
});