Skip to content

Commit 75fa997

Browse files
committed
(feat:JS): add types
1 parent 67d0354 commit 75fa997

File tree

5 files changed

+56
-249
lines changed

5 files changed

+56
-249
lines changed

.vscode/index.css

Lines changed: 0 additions & 130 deletions
This file was deleted.

.vscode/index.html

Lines changed: 0 additions & 54 deletions
This file was deleted.

dist/index.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
"use strict";
21
document.addEventListener('DOMContentLoaded', () => {
32
const btns = document.querySelectorAll("[data-value]");
43
const historyElement = document.querySelector(".computation-history");
@@ -51,20 +50,21 @@ document.addEventListener('DOMContentLoaded', () => {
5150
computationHistoryParent.classList.toggle("visility");
5251
});
5352
let data = [];
53+
// let data: string[] = [];
5454
btns.forEach((btn) => {
5555
btn.addEventListener("click", function (e) {
5656
let buttonValue = e.target && e.target.dataset.value;
57-
insertOpeningParenthesis(buttonValue);
58-
insertClosingParenthesis(buttonValue);
59-
deleteEverythingFromScreen(buttonValue);
57+
insertOpeningParenthesis(String(buttonValue));
58+
insertClosingParenthesis(String(buttonValue));
59+
deleteEverythingFromScreen(String(buttonValue));
6060
toggleSign(buttonValue);
61-
canUserAddDot(buttonValue);
62-
userClicksOnEqualButton(buttonValue);
63-
handlingZeroFollowedByAdecimal(buttonValue);
64-
removesDecimalPointIfPrecededByAnOperator(buttonValue);
65-
handleNumberButton(buttonValue);
66-
deteLastEntry(buttonValue);
67-
convertToPercentage(buttonValue);
61+
canUserAddDot(String(buttonValue));
62+
userClicksOnEqualButton(String(buttonValue));
63+
handlingZeroFollowedByAdecimal(String(buttonValue));
64+
removesDecimalPointIfPrecededByAnOperator(String(buttonValue));
65+
handleNumberButton(String(buttonValue));
66+
deteLastEntry(String(buttonValue));
67+
convertToPercentage(String(buttonValue));
6868
});
6969
});
7070
// forEach ends & functions creations begins
@@ -228,7 +228,7 @@ document.addEventListener('DOMContentLoaded', () => {
228228
function userClicksOnEqualButton(button) {
229229
if (button === "=") {
230230
try {
231-
const replacedArray = data.map((item) => item === "x" ? "*" : item === "÷" ? "/" : item);
231+
const replacedArray = data.map((item) => item === "x" ? "*" : item === "÷" ? "/" : String(item));
232232
if (areYouDividingdZeroByZero(replacedArray)) {
233233
screenElement.innerText = "0÷0 is an invalid format. Press AC";
234234
}
@@ -248,24 +248,20 @@ document.addEventListener('DOMContentLoaded', () => {
248248
togglesClearHistoryButton(historyElement);
249249
}
250250
}
251-
catch (e) {
252-
console.error(e);
253-
screenElement.innerText = `${e.name} press AC`;
254-
}
255-
}
256-
}
257-
function areYouDivindingByZero(array) {
258-
for (let i = ZERO; i < array.length - 2; i++) {
259-
if (!isNaN(Number(array[i])) &&
260-
array[i + 1] === "/" &&
261-
array[i + 2] === "0") {
262-
return true;
251+
catch (e) { // unknown is safer than any because TypeScript forces you to narrow the type before using it (e.g., with instanceof checks).This prevents accidental assumptions about the error structure.
252+
if (e instanceof Error) {
253+
console.error(e.message);
254+
screenElement.innerText = `${e.name} press AC`;
255+
}
256+
else {
257+
console.error("An unknown error occurred:", e);
258+
screenElement.innerText = "Error press AC";
259+
}
263260
}
264261
}
265-
return false;
266262
}
267263
function areYouDividingdZeroByZero(array) {
268-
for (let i = ZERO; i < array.length - 2; i++) {
264+
for (let i = 0; i < array.length - 2; i++) {
269265
if (array[i] === "0" && array[i + 1] === "/" && array[i + 2] === "0") {
270266
return true;
271267
}

0 commit comments

Comments
 (0)