Skip to content

Commit 08ba4ec

Browse files
committed
feat(JS) add 'screen.innerText = result' to else statement in try
1 parent 3d5821c commit 08ba4ec

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

index.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,17 @@ btns.forEach((btn) => {
7878
try {
7979
const replacedArray = data.map((item) => (item === "x" ? "*" : item === "÷" ? "/" : item));
8080
// Check if the expression involves 0/0
81-
if (hasZeroDividedByZero(replacedArray)) {
82-
screen.innerText = "Invalid format used. You cannot divide by zero";
81+
if (areYouDividingdZeroByZero(replacedArray)) {
82+
screen.innerText = "0/0 is an invalid format used. Press AC";
83+
} if (areYouDivindingByZero(replacedArray)) {
84+
screen.innerText = "You cannot divide by zero. Press AC";
85+
// debugger
8386
} else {
8487
let result = eval(replacedArray.join(""));
85-
console.log(result);
88+
console.log(result)
8689
displayResult(replacedArray, result);
87-
divideByZero(screen, result);
90+
screen.innerText = result
91+
// divideByZero(screen, result);
8892
data = [];
8993
data.push(result);
9094
}
@@ -94,13 +98,21 @@ btns.forEach((btn) => {
9498
}
9599

96100

97-
function divideByZero(display, outcome) {
98-
outcome === Infinity
99-
? (display.innerText = "Math Error. Cannot divide by zero")
100-
: (display.innerText = outcome);
101+
function areYouDivindingByZero(array) {
102+
for (let i = 0; i < array.length - 2; i++) {
103+
if (!isNaN(Number(array[i])) && array[i + 1] === "/" && array[i + 2] === "0") {
104+
return true;
105+
}
106+
}
107+
return false;
101108
}
109+
110+
111+
112+
113+
102114

103-
function hasZeroDividedByZero(array) {
115+
function areYouDividingdZeroByZero(array) {
104116
for (let i = 0; i < array.length - 2; i++) {
105117
if (array[i] === "0" && array[i + 1] === "/" && array[i + 2] === "0") {
106118
return true;

0 commit comments

Comments
 (0)