Skip to content

Commit cfa45db

Browse files
committed
(JS): fix bug/infinnite loop with local storage
1 parent 18ae456 commit cfa45db

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

index.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ document.addEventListener('keydown', handleKeyPress);
1616
// Add this at the beginning of your script
1717
window.addEventListener("DOMContentLoaded", () => {
1818
const history = getHistoryFromLocalStorage();
19-
history.forEach((entry) => {
20-
createHistoryList(entry, historyElement);
21-
});
19+
createHistoryList(history, historyElement);
20+
2221
});
2322

2423

@@ -232,18 +231,21 @@ function userClicksOnEqualButton(button) {
232231
screen.innerText = "0÷0 is an invalid format. Press AC";
233232
} else {
234233
let result = eval(replacedArray.join(""));
235-
let historyEntries = [[...replacedArray, "=", result]]; // 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.
234+
const history = getHistoryFromLocalStorage()
235+
history.push([...replacedArray, "=", result]); // 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.
236236
replacedArray.splice(replacedArray.length, 0, "=", result);
237237
displayResult(replacedArray, result);
238238
screen.innerText = !Number.isFinite(result)
239239
? "You cannot divide by zero. Press AC"
240240
: result;
241241
data = [];
242242
data.push(result);
243-
createHistoryList(historyEntries, historyElement, history);
243+
setHistoryToLocalStorage(history)
244+
createHistoryList(history, historyElement);
244245
togglesClearHistoryButton(historyElement, clearHistoryBtn);
245246
}
246247
} catch (e) {
248+
console.error(e)
247249
screen.innerText = `${e.name} press AC`;
248250
}
249251
}
@@ -287,24 +289,21 @@ function displayResult(array, outcome) {
287289
// });
288290
// }
289291

290-
function createHistoryList(array, element) {
291-
const history = getHistoryFromLocalStorage();
292-
history.push(array);
293-
setHistoryToLocalStorage(history);
294-
295-
element.innerHTML += `<li> ${array.join(" ")}</li>`;
296-
297-
if (element.childElementCount > 10) {
298-
element.firstElementChild.remove();
299-
}
292+
function createHistoryList(entries, element) {
293+
element.innerHTML = "";
294+
entries.slice(-10).reverse().forEach((entry) => {
295+
const listItem = document.createElement("li");
296+
listItem.textContent = entry;
297+
element.appendChild(listItem);
298+
});
300299
}
301300

302301
function getHistoryFromLocalStorage() {
303302
return JSON.parse(localStorage.getItem("calculatorHistory")) || [];
304303
}
305304

306305
function setHistoryToLocalStorage(history) {
307-
localStorage.setItem("calculatorHistory", JSON.stringify(history));
306+
localStorage.setItem("calculatorHistory", JSON.stringify(history.slice(-10)));
308307
}
309308

310309

0 commit comments

Comments
 (0)