Skip to content

Commit

Permalink
Merge pull request #2 from josueJURE/dividing-by-zero-3.0
Browse files Browse the repository at this point in the history
Dividing by zero 3.0
  • Loading branch information
josueJURE authored Dec 19, 2023
2 parents 3d5821c + 55f0e93 commit e78a770
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ let data = [];
btns.forEach((btn) => {
btn.addEventListener("click", function (e) {
let buttonValue = e.target.dataset.value;
// if (buttonValue === "(") {
// let isOpenParenthesis = true;
// for (let i = data.length - 1; i >= 0; i--) {
// if (/^\d$/.test(data[i])) { // Use /^\d$/ to match a single digit
// isOpenParenthesis = false;
// break; // Exit the loop as soon as a digit is found
// }
// }
// // Rest of your code...
// }

if (buttonValue === "(") {
let isOpenparenthesis = true;
Expand Down Expand Up @@ -60,8 +50,12 @@ btns.forEach((btn) => {
if (data.slice(-1)[0] === ".") {
data.pop();
}
buttonValue === "*" ? buttonValue = "x" : buttonValue === "/" ? buttonValue = "÷" : buttonValue;

buttonValue === "*"
? (buttonValue = "x")
: buttonValue === "/"
? (buttonValue = "÷")
: buttonValue;

data.push(buttonValue);
screen.innerText = data.join("");
}
Expand All @@ -73,42 +67,57 @@ btns.forEach((btn) => {
canUserAddDot();
}


if (buttonValue === "=") {
try {
const replacedArray = data.map((item) => (item === "x" ? "*" : item === "÷" ? "/" : item));
const replacedArray = data.map((item) =>
item === "x" ? "*" : item === "÷" ? "/" : item
);
// Check if the expression involves 0/0
if (hasZeroDividedByZero(replacedArray)) {
screen.innerText = "Invalid format used. You cannot divide by zero";
// if (areYouDivindingByZero(replacedArray)) {
// screen.innerText = "You cannot divide by zero. Press AC";
// }

if (areYouDividingdZeroByZero(replacedArray)) {
screen.innerText = "0÷0 is an invalid format. Press AC";
} else {
let result = eval(replacedArray.join(""));
console.log(result);
displayResult(replacedArray, result);
divideByZero(screen, result);
screen.innerText =
result === Infinity
? "You cannot divide by zero. Press AC"
: result;
// divideByZero(screen, result);
data = [];
data.push(result);
}
} catch (e) {
screen.innerText = `${e.name} press AC`;
}
}


function divideByZero(display, outcome) {
outcome === Infinity
? (display.innerText = "Math Error. Cannot divide by zero")
: (display.innerText = outcome);
function areYouDivindingByZero(array) {
for (let i = 0; i < array.length - 2; i++) {
if (
!isNaN(Number(array[i])) &&
array[i + 1] === "/" &&
array[i + 2] === "0"
) {
return true;
}
}
return false;
}

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

function displayResult(array, outcome) {
array = [];
array.push(outcome);
Expand Down Expand Up @@ -175,12 +184,10 @@ function toggleSign() {
if (!isNaN(currentValue)) {
// If it's a number, toggle its sign
currentValue = -currentValue;
data = data.slice(0, start).concat(currentValue.toString().split(""), data.slice(end));
data = data
.slice(0, start)
.concat(currentValue.toString().split(""), data.slice(end));
screen.innerText = data.join("");
}
}
}




0 comments on commit e78a770

Please sign in to comment.