Skip to content

Commit d771058

Browse files
authored
Merge pull request #32 from josueJURE/clear-history-button
Clear history button
2 parents 28379e3 + 2c4b66a commit d771058

File tree

2 files changed

+20
-45
lines changed

2 files changed

+20
-45
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<div class="computation-history-parent">
3939
<h3>history</h3>
4040
<ul class="computation-history" aria-label="Computation History"></ul>
41-
<button class="clear-history-btn" aria-label="Clear History">clear history</button>
41+
<button class="clear-history-btn display" aria-label="Clear History">clear history</button>
4242

4343
</div>
4444

index.js

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,24 @@ const computationHistoryParent = document.querySelector(".computation-history-pa
77
const operators = document.querySelectorAll("[data-operator]");
88
const clearHistoryBtn = document.querySelector(".clear-history-btn");
99

10-
clearHistoryBtn.addEventListener("click", () => {historyElement.innerHTML = "";
10+
clearHistoryBtn.addEventListener("click", () => {
11+
historyElement.innerHTML = "";
1112
});
1213

13-
document.addEventListener('keydown', handleKeyPress);
14-
14+
document.addEventListener("keydown", handleKeyPress);
1515

1616
// Add this at the beginning of your script
1717
window.addEventListener("DOMContentLoaded", () => {
1818
const history = getHistoryFromLocalStorage();
19-
createHistoryList(history, historyElement);
20-
19+
createHistoryList(history, historyElement);
2120
});
2221

23-
2422
function handleKeyPress(event) {
25-
2623
const key = event.key;
2724
const button = document.querySelector(`[data-value="${key}"]`);
28-
29-
console.log(key)
30-
31-
32-
3325
if (button) {
3426
button.click(); // Trigger the click event for the corresponding button
35-
}
27+
}
3628

3729
if (event.code === "Backspace") {
3830
let newArray = data.slice(ZERO, -1);
@@ -43,13 +35,11 @@ function handleKeyPress(event) {
4335
}
4436
}
4537

46-
if(event.code === "Enter") {
38+
if (event.code === "Enter") {
4739
userClicksOnEqualButton("=");
4840
}
49-
5041
}
5142

52-
5343
const operatorRegex = /[\/*\-+]/;
5444
const ZERO = 0;
5545
const ZERO_DOT = "0.";
@@ -97,8 +87,6 @@ function convertToPercentage(button) {
9787
}
9888
}
9989

100-
101-
10290
function deteLastEntry(button) {
10391
if (button === "DE") {
10492
let newArray = data.slice(ZERO, -1);
@@ -229,21 +217,21 @@ function userClicksOnEqualButton(button) {
229217
screen.innerText = "0÷0 is an invalid format. Press AC";
230218
} else {
231219
let result = eval(replacedArray.join(""));
232-
const history = getHistoryFromLocalStorage()
233-
history.push([...replacedArray, "=", result].join('').split(',')); // Used slice() at first. But slice() is not sufficient because it only creates a shallow copy of the array, and modifications to the new array will still affect the original array. The spread syntax ([...replacedArray]), which creates a shallow copy as well, is a concise way to create a new array with the same elements as the existing array. While ensuring that modifications to historyEntries do not affect replacedArray, and vice versa
220+
const history = getHistoryFromLocalStorage();
221+
history.push([...replacedArray, "=", result].join("").split(",")); // Used slice() at first. But slice() is not sufficient because it only creates a shallow copy of the array, and modifications to the new array will still affect the original array. The spread syntax ([...replacedArray]), which creates a shallow copy as well, is a concise way to create a new array with the same elements as the existing array. While ensuring that modifications to historyEntries do not affect replacedArray, and vice versa
234222
replacedArray.splice(replacedArray.length, 0, "=", result);
235223
displayResult(replacedArray, result);
236224
screen.innerText = !Number.isFinite(result)
237225
? "You cannot divide by zero. Press AC"
238226
: result;
239227
data = [];
240228
data.push(result);
241-
setHistoryToLocalStorage(history)
229+
setHistoryToLocalStorage(history);
242230
createHistoryList(history, historyElement);
243231
togglesClearHistoryButton(historyElement, clearHistoryBtn);
244232
}
245233
} catch (e) {
246-
console.error(e)
234+
console.error(e);
247235
screen.innerText = `${e.name} press AC`;
248236
}
249237
}
@@ -276,24 +264,16 @@ function displayResult(array, outcome) {
276264
array.push(outcome);
277265
}
278266

279-
// function createHistoryList(array, element) {
280-
281-
// array.forEach((entry) => {
282-
// history.push(entry);
283-
// element.innerHTML += `<li> ${entry.join(" ")}</li>`;
284-
// if (element.childElementCount > HISTORY_LIMIT) {
285-
// element.firstElementChild.remove();
286-
// }
287-
// });
288-
// }
289-
290267
function createHistoryList(entries, element) {
291268
element.innerHTML = "";
292-
entries.slice(-10).reverse().forEach((entry) => {
293-
const listItem = document.createElement("li");
294-
listItem.textContent = entry;
295-
element.appendChild(listItem);
296-
});
269+
entries
270+
.slice(-10)
271+
.reverse()
272+
.forEach((entry) => {
273+
const listItem = document.createElement("li");
274+
listItem.textContent = entry;
275+
element.appendChild(listItem);
276+
});
297277
}
298278

299279
function getHistoryFromLocalStorage() {
@@ -304,10 +284,6 @@ function setHistoryToLocalStorage(history) {
304284
localStorage.setItem("calculatorHistory", JSON.stringify(history.slice(-10)));
305285
}
306286

307-
308-
309-
310-
311287
clearHistoryBtn.addEventListener("click", () => {
312288
historyElement.innerHTML = "";
313289
clearHistoryInLocalStorage();
@@ -319,7 +295,6 @@ function clearHistoryInLocalStorage() {
319295
}
320296

321297
function togglesClearHistoryButton(element, btn) {
322-
const history = getHistoryFromLocalStorage();
323-
btn.classList.toggle("display", element.childElementCount > 0);
298+
clearHistoryBtn.classList.toggle("display", element.childElementCount > 0);
324299
}
325300
// functions creations ends

0 commit comments

Comments
 (0)