Skip to content

Commit

Permalink
feat(JS) delete commented out code
Browse files Browse the repository at this point in the history
  • Loading branch information
josueJURE committed Dec 19, 2023
1 parent 56024bb commit 55f0e93
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 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,22 +67,26 @@ 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 (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 {
} else {
let result = eval(replacedArray.join(""));
console.log(result)
console.log(result);
displayResult(replacedArray, result);
screen.innerText = result === Infinity ? "You cannot divide by zero. Press AC" : result
screen.innerText =
result === Infinity
? "You cannot divide by zero. Press AC"
: result;
// divideByZero(screen, result);
data = [];
data.push(result);
Expand All @@ -97,21 +95,19 @@ btns.forEach((btn) => {
screen.innerText = `${e.name} press AC`;
}
}


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






function areYouDividingdZeroByZero(array) {
for (let i = 0; i < array.length - 2; i++) {
Expand All @@ -121,7 +117,7 @@ btns.forEach((btn) => {
}
return false;
}

function displayResult(array, outcome) {
array = [];
array.push(outcome);
Expand Down Expand Up @@ -188,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 55f0e93

Please sign in to comment.