Skip to content

Commit 4a8ad94

Browse files
committed
feat(JS): add a comment
1 parent f02b5e8 commit 4a8ad94

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ const historyElement = document.querySelector(".computation-history");
33
let screen = document.querySelector("[data-screen]");
44
const historyBtn = document.querySelector(".history-btn");
55
const slidingPart = document.querySelector(".sliding-part");
6-
const computationHistoryParent = document.querySelector(
7-
".computation-history-parent"
8-
);
6+
const computationHistoryParent = document.querySelector(".computation-history-parent");
97
const operators = document.querySelectorAll("[data-operator]");
108
const clearHistoryBtn = document.querySelector(".clear-history-btn");
119

12-
clearHistoryBtn.addEventListener("click", () => {
13-
historyElement.innerHTML = "";
10+
clearHistoryBtn.addEventListener("click", () => {historyElement.innerHTML = "";
1411
});
1512

1613
const operatorRegex = /[\/*\-+]/;
@@ -194,7 +191,7 @@ function userClicksOnEqualButton(button) {
194191
screen.innerText = "0÷0 is an invalid format. Press AC";
195192
} else {
196193
let result = eval(replacedArray.join(""));
197-
let historyEntries = [[...replacedArray, "=", result]];
194+
let historyEntries = [[...replacedArray, "=", result]]; // Used slice() at firest. 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.
198195
replacedArray.splice(replacedArray.length, 0, "=", result);
199196
displayResult(replacedArray, result);
200197
screen.innerText = !Number.isFinite(result)

0 commit comments

Comments
 (0)