Skip to content

Commit e2cedab

Browse files
authored
Merge pull request #30 from josueJURE/local-storage
Local storage
2 parents b11d2b3 + cfa45db commit e2cedab

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

index.js

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ clearHistoryBtn.addEventListener("click", () => {historyElement.innerHTML = "";
1212

1313
document.addEventListener('keydown', handleKeyPress);
1414

15+
16+
// Add this at the beginning of your script
17+
window.addEventListener("DOMContentLoaded", () => {
18+
const history = getHistoryFromLocalStorage();
19+
createHistoryList(history, historyElement);
20+
21+
});
22+
23+
1524
function handleKeyPress(event) {
1625

1726
const key = event.key;
@@ -222,18 +231,21 @@ function userClicksOnEqualButton(button) {
222231
screen.innerText = "0÷0 is an invalid format. Press AC";
223232
} else {
224233
let result = eval(replacedArray.join(""));
225-
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.
226236
replacedArray.splice(replacedArray.length, 0, "=", result);
227237
displayResult(replacedArray, result);
228238
screen.innerText = !Number.isFinite(result)
229239
? "You cannot divide by zero. Press AC"
230240
: result;
231241
data = [];
232242
data.push(result);
233-
createHistoryList(historyEntries, historyElement, history);
243+
setHistoryToLocalStorage(history)
244+
createHistoryList(history, historyElement);
234245
togglesClearHistoryButton(historyElement, clearHistoryBtn);
235246
}
236247
} catch (e) {
248+
console.error(e)
237249
screen.innerText = `${e.name} press AC`;
238250
}
239251
}
@@ -266,21 +278,50 @@ function displayResult(array, outcome) {
266278
array.push(outcome);
267279
}
268280

269-
function createHistoryList(array, element, history) {
270-
array.forEach((entry) => {
271-
history.push(entry);
272-
element.innerHTML += `<li> ${entry.join(" ")}</li>`;
273-
if (element.childElementCount > HISTORY_LIMIT) {
274-
element.firstElementChild.remove();
275-
}
281+
// function createHistoryList(array, element) {
282+
283+
// array.forEach((entry) => {
284+
// history.push(entry);
285+
// element.innerHTML += `<li> ${entry.join(" ")}</li>`;
286+
// if (element.childElementCount > HISTORY_LIMIT) {
287+
// element.firstElementChild.remove();
288+
// }
289+
// });
290+
// }
291+
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);
276298
});
277299
}
300+
301+
function getHistoryFromLocalStorage() {
302+
return JSON.parse(localStorage.getItem("calculatorHistory")) || [];
303+
}
304+
305+
function setHistoryToLocalStorage(history) {
306+
localStorage.setItem("calculatorHistory", JSON.stringify(history.slice(-10)));
307+
}
308+
309+
310+
311+
312+
278313
clearHistoryBtn.addEventListener("click", () => {
279314
historyElement.innerHTML = "";
315+
clearHistoryInLocalStorage();
280316
togglesClearHistoryButton(historyElement, clearHistoryBtn);
281317
});
282318

319+
function clearHistoryInLocalStorage() {
320+
localStorage.removeItem("calculatorHistory");
321+
}
322+
283323
function togglesClearHistoryButton(element, btn) {
324+
const history = getHistoryFromLocalStorage();
284325
btn.classList.toggle("display", element.childElementCount > 0);
285326
}
286327
// functions creations ends

0 commit comments

Comments
 (0)