From c56ec06ca9e3fe294cc5c249c2618b04c4bd5c71 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Tue, 19 Dec 2023 15:23:35 +0000 Subject: [PATCH 01/19] feat(JS) create insertOpeningParenthesis() --- index.js | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index e6e24e4..5e99918 100644 --- a/index.js +++ b/index.js @@ -8,26 +8,11 @@ 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])) { - isOpenparenthesis = false; - break; - } - if (data[i] === ")") { - isOpenparenthesis = false; - break; - } - if (/[\/*\-+]/.test(data[i])) { - break; - } - } - if (isOpenparenthesis) { - data.push("("); - } - screen.innerText = data.join(""); - } + insertOpeningParenthesis() + + + + if (buttonValue === ")") { data.push(")"); @@ -130,6 +115,29 @@ btns.forEach((btn) => { if (buttonValue === "DE") { deteLastEntry(); } + function insertOpeningParenthesis() { + if (buttonValue === "(") { + let isOpenparenthesis = true; + for (let i = data.length - 1; i >= 0; i--) { + if (/^\d$/.test(data[i])) { + isOpenparenthesis = false; + break; + } + if (data[i] === ")") { + isOpenparenthesis = false; + break; + } + if (/[\/*\-+]/.test(data[i])) { + break; + } + } + if (isOpenparenthesis) { + data.push("("); + } + screen.innerText = data.join(""); + } + + } }); }); From f662d5e2ae5444aa242fe79ca397adb145a2ff07 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Tue, 19 Dec 2023 15:34:14 +0000 Subject: [PATCH 02/19] feat(JS) create insertClosingParenthesis() --- index.js | 67 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index 5e99918..7e77035 100644 --- a/index.js +++ b/index.js @@ -8,16 +8,19 @@ btns.forEach((btn) => { btn.addEventListener("click", function (e) { let buttonValue = e.target.dataset.value; - insertOpeningParenthesis() + insertOpeningParenthesis(buttonValue); + + insertClosingParenthesis(buttonValue) + + + + - if (buttonValue === ")") { - data.push(")"); - screen.innerText = data.join(""); - } + if (buttonValue === "AC") { deleteEverythingFromScreen(); @@ -115,29 +118,7 @@ btns.forEach((btn) => { if (buttonValue === "DE") { deteLastEntry(); } - function insertOpeningParenthesis() { - if (buttonValue === "(") { - let isOpenparenthesis = true; - for (let i = data.length - 1; i >= 0; i--) { - if (/^\d$/.test(data[i])) { - isOpenparenthesis = false; - break; - } - if (data[i] === ")") { - isOpenparenthesis = false; - break; - } - if (/[\/*\-+]/.test(data[i])) { - break; - } - } - if (isOpenparenthesis) { - data.push("("); - } - screen.innerText = data.join(""); - } - - } + }); }); @@ -199,3 +180,33 @@ function toggleSign() { } } } + +function insertOpeningParenthesis(button) { + if (button === "(") { + let isOpenparenthesis = true; + for (let i = data.length - 1; i >= 0; i--) { + if (/^\d$/.test(data[i])) { + isOpenparenthesis = false; + break; + } + if (data[i] === ")") { + isOpenparenthesis = false; + break; + } + if (/[\/*\-+]/.test(data[i])) { + break; + } + } + if (isOpenparenthesis) { + data.push("("); + } + screen.innerText = data.join(""); + } +} + +function insertClosingParenthesis(button) { + if (button === ")") { + data.push(")"); + screen.innerText = data.join(""); + } +} From 9a55705e2da3c747d8972bc25cec6989e8dd3197 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Tue, 19 Dec 2023 15:40:19 +0000 Subject: [PATCH 03/19] feat(JS) refactor deleteEverythingFromScreen() --- index.js | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 7e77035..99273c3 100644 --- a/index.js +++ b/index.js @@ -10,21 +10,10 @@ btns.forEach((btn) => { insertOpeningParenthesis(buttonValue); - insertClosingParenthesis(buttonValue) + insertClosingParenthesis(buttonValue); - + deleteEverythingFromScreen(buttonValue); - - - - - - - - - if (buttonValue === "AC") { - deleteEverythingFromScreen(); - } if (Number(buttonValue) === 0 && screen.innerText.startsWith("0.")) { screen.innerText += buttonValue; @@ -118,7 +107,6 @@ btns.forEach((btn) => { if (buttonValue === "DE") { deteLastEntry(); } - }); }); @@ -153,10 +141,12 @@ function canUserAddDot() { screen.innerText = data.join(" "); } -function deleteEverythingFromScreen() { - screen.innerText = ""; - data = []; - screen.innerText = 0; +function deleteEverythingFromScreen(button) { + if (button === "AC") { + screen.innerText = ""; + data = []; + screen.innerText = 0; + } } function toggleSign() { From c7435e8e076a0d071be4da8030874cec96e1a2a7 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Tue, 19 Dec 2023 15:46:45 +0000 Subject: [PATCH 04/19] feat(JS) refactor canUserAddDot(buttonValue) --- index.js | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 99273c3..23b5b5c 100644 --- a/index.js +++ b/index.js @@ -40,9 +40,9 @@ btns.forEach((btn) => { toggleSign(); } - if (buttonValue === ".") { - canUserAddDot(); - } + + canUserAddDot(buttonValue); + if (buttonValue === "=") { try { @@ -119,26 +119,28 @@ function deteLastEntry() { } } -function canUserAddDot() { - var dotAllowed = true; - for (var i = data.length - 1; i >= 0; i--) { - console.log("data > " + data[i]); - if (data[i] === ".") { - dotAllowed = false; - break; - } else if (/[\/*\-+]/.test(data[i])) { - break; +function canUserAddDot(button) { + if(button === ".") { + var dotAllowed = true; + for (var i = data.length - 1; i >= 0; i--) { + console.log("data > " + data[i]); + if (data[i] === ".") { + dotAllowed = false; + break; + } else if (/[\/*\-+]/.test(data[i])) { + break; + } } - } - if (dotAllowed) { - if (data.length == 0) { - data.push("0"); - } else if (/[\/*\-+]/.test(data[data.length - 1])) { - data.push("0"); + if (dotAllowed) { + if (data.length == 0) { + data.push("0"); + } else if (/[\/*\-+]/.test(data[data.length - 1])) { + data.push("0"); + } + data.push("."); } - data.push("."); + screen.innerText = data.join(" "); } - screen.innerText = data.join(" "); } function deleteEverythingFromScreen(button) { From b77f2e16a637bd7f04abd0c29baef0484d1b4bb4 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Tue, 19 Dec 2023 15:56:21 +0000 Subject: [PATCH 05/19] feat(JS) refactor toggleSign() --- index.js | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index 23b5b5c..e39e36d 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,8 @@ btns.forEach((btn) => { deleteEverythingFromScreen(buttonValue); + toggleSign(buttonValue); + if (Number(buttonValue) === 0 && screen.innerText.startsWith("0.")) { screen.innerText += buttonValue; @@ -36,9 +38,9 @@ btns.forEach((btn) => { data.push(buttonValue); screen.innerText = data.join(""); } - if (buttonValue === "minus") { - toggleSign(); - } + + + canUserAddDot(buttonValue); @@ -110,6 +112,9 @@ btns.forEach((btn) => { }); }); +// end of forEach() statement + + function deteLastEntry() { let newArray = data.slice(0, -1); screen.innerText = newArray.join(""); @@ -151,26 +156,30 @@ function deleteEverythingFromScreen(button) { } } -function toggleSign() { - let currentExpression = data.join(""); - let reversedExpression = currentExpression.split("").reverse().join(""); - let match = reversedExpression.match(/(\d+(\.\d+)?)|(\D+)/); // Match a number or non-digit - // debugger - - if (match) { - let start = currentExpression.length - match[0].length; - let end = currentExpression.length; - let currentValue = Number(match[0]); - - 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)); - screen.innerText = data.join(""); +function toggleSign(button) { + if(button === "minus") { + let currentExpression = data.join(""); + let reversedExpression = currentExpression.split("").join(""); + let match = reversedExpression.match(/(\d+(\.\d+)?)|(\D+)/); // Match a number or non-digit + // debugger + + if (match) { + let start = currentExpression.length - match[0].length; + let end = currentExpression.length; + let currentValue = Number(match[0]); + + 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)); + screen.innerText = data.join(""); + } } + } + } function insertOpeningParenthesis(button) { From fdefee33f755dd533776aec2e7f0a70f0154ba23 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Tue, 19 Dec 2023 16:02:10 +0000 Subject: [PATCH 06/19] feat(JS) creates userClicksOnEqualButton() --- index.js | 62 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index e39e36d..5afa4d6 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,10 @@ btns.forEach((btn) => { toggleSign(buttonValue); + canUserAddDot(buttonValue); + + userClicksOnEqualButton(buttonValue); + if (Number(buttonValue) === 0 && screen.innerText.startsWith("0.")) { screen.innerText += buttonValue; @@ -42,38 +46,38 @@ btns.forEach((btn) => { +function userClicksOnEqualButton(button) { + if (button === "=") { + try { + 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 { + let result = eval(replacedArray.join("")); + console.log(result); + displayResult(replacedArray, 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`; + } + } - canUserAddDot(buttonValue); +} - if (buttonValue === "=") { - try { - 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 { - let result = eval(replacedArray.join("")); - console.log(result); - displayResult(replacedArray, 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 areYouDivindingByZero(array) { for (let i = 0; i < array.length - 2; i++) { From 3d908a8b7c3c4e9fb588244427b749512cc99b60 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Tue, 19 Dec 2023 17:26:15 +0000 Subject: [PATCH 07/19] feat(JS) create handlingZeroFollowedByAdecimal() --- index.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 5afa4d6..3790fed 100644 --- a/index.js +++ b/index.js @@ -21,9 +21,10 @@ btns.forEach((btn) => { userClicksOnEqualButton(buttonValue); - if (Number(buttonValue) === 0 && screen.innerText.startsWith("0.")) { - screen.innerText += buttonValue; - } + handlingZeroFollowedByAdecimal(buttonValue) + + + if (!isNaN(Number(buttonValue))) { screen.innerText = buttonValue; data.push(screen.innerText); @@ -215,3 +216,9 @@ function insertClosingParenthesis(button) { screen.innerText = data.join(""); } } + +function handlingZeroFollowedByAdecimal(button) { + if (Number(button) === 0 && screen.innerText.startsWith("0.")) { + screen.innerText += buttonValue; + } +} From 4a95ab59612bb06025b593ae8fda8e28837a659d Mon Sep 17 00:00:00 2001 From: josueJURE Date: Tue, 19 Dec 2023 18:00:47 +0000 Subject: [PATCH 08/19] feat(JS) removesDecimalPointIfPrecededByAnOperator(button) --- index.js | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 3790fed..a09c7e1 100644 --- a/index.js +++ b/index.js @@ -20,8 +20,14 @@ btns.forEach((btn) => { userClicksOnEqualButton(buttonValue); + handlingZeroFollowedByAdecimal(buttonValue); - handlingZeroFollowedByAdecimal(buttonValue) + removesDecimalPointIfPrecededByAnOperator(buttonValue) + + + + + @@ -30,19 +36,9 @@ btns.forEach((btn) => { data.push(screen.innerText); screen.innerText = data.join(""); } - if (/[\/*\-+]/.test(buttonValue)) { - if (data.slice(-1)[0] === ".") { - data.pop(); - } - buttonValue === "*" - ? (buttonValue = "x") - : buttonValue === "/" - ? (buttonValue = "÷") - : buttonValue; - data.push(buttonValue); - screen.innerText = data.join(""); - } + + @@ -222,3 +218,19 @@ function handlingZeroFollowedByAdecimal(button) { screen.innerText += buttonValue; } } + +function removesDecimalPointIfPrecededByAnOperator(button) { + if (/[\/*\-+]/.test(button)) { + if (data.slice(-1)[0] === ".") { + data.pop(); + } + button === "*" + ? (button = "x") + : button === "/" + ? (button = "÷") + : button; + + data.push(button); + screen.innerText = data.join(""); + } +} From 30d8d931c330bfb2e257c6a7e9365209d9b3f7b4 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Tue, 19 Dec 2023 18:16:12 +0000 Subject: [PATCH 09/19] feat(JS) create handleNumberButton() --- index.js | 108 +++++++++++++++++++++++-------------------------------- 1 file changed, 45 insertions(+), 63 deletions(-) diff --git a/index.js b/index.js index a09c7e1..ce3a7af 100644 --- a/index.js +++ b/index.js @@ -22,59 +22,40 @@ btns.forEach((btn) => { handlingZeroFollowedByAdecimal(buttonValue); - removesDecimalPointIfPrecededByAnOperator(buttonValue) - - - - - - - - - if (!isNaN(Number(buttonValue))) { - screen.innerText = buttonValue; - data.push(screen.innerText); - screen.innerText = data.join(""); - } - - - - - - - -function userClicksOnEqualButton(button) { - if (button === "=") { - try { - 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 { - let result = eval(replacedArray.join("")); - console.log(result); - displayResult(replacedArray, result); - screen.innerText = - result === Infinity - ? "You cannot divide by zero. Press AC" - : result; - // divideByZero(screen, result); - data = []; - data.push(result); + removesDecimalPointIfPrecededByAnOperator(buttonValue); + + handleNumberButton(buttonValue); + + function userClicksOnEqualButton(button) { + if (button === "=") { + try { + 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 { + let result = eval(replacedArray.join("")); + console.log(result); + displayResult(replacedArray, 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`; + } } - } catch (e) { - screen.innerText = `${e.name} press AC`; } - } - -} - - - function areYouDivindingByZero(array) { for (let i = 0; i < array.length - 2; i++) { @@ -115,7 +96,6 @@ function userClicksOnEqualButton(button) { // end of forEach() statement - function deteLastEntry() { let newArray = data.slice(0, -1); screen.innerText = newArray.join(""); @@ -126,7 +106,7 @@ function deteLastEntry() { } function canUserAddDot(button) { - if(button === ".") { + if (button === ".") { var dotAllowed = true; for (var i = data.length - 1; i >= 0; i--) { console.log("data > " + data[i]); @@ -158,17 +138,17 @@ function deleteEverythingFromScreen(button) { } function toggleSign(button) { - if(button === "minus") { + if (button === "minus") { let currentExpression = data.join(""); let reversedExpression = currentExpression.split("").join(""); let match = reversedExpression.match(/(\d+(\.\d+)?)|(\D+)/); // Match a number or non-digit // debugger - + if (match) { let start = currentExpression.length - match[0].length; let end = currentExpression.length; let currentValue = Number(match[0]); - + if (!isNaN(currentValue)) { // If it's a number, toggle its sign currentValue = -currentValue; @@ -178,9 +158,7 @@ function toggleSign(button) { screen.innerText = data.join(""); } } - } - } function insertOpeningParenthesis(button) { @@ -224,13 +202,17 @@ function removesDecimalPointIfPrecededByAnOperator(button) { if (data.slice(-1)[0] === ".") { data.pop(); } - button === "*" - ? (button = "x") - : button === "/" - ? (button = "÷") - : button; + button === "*" ? (button = "x") : button === "/" ? (button = "÷") : button; data.push(button); screen.innerText = data.join(""); } } + +function handleNumberButton(button) { + if (!isNaN(Number(button))) { + screen.innerText = button; + data.push(screen.innerText); + screen.innerText = data.join(""); + } +} From 8620bafd8202a6d1a2033a556cf9e56047f00abf Mon Sep 17 00:00:00 2001 From: josueJURE Date: Tue, 19 Dec 2023 18:20:40 +0000 Subject: [PATCH 10/19] feat(JS) rewrite deteLastEntry() --- index.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index ce3a7af..afbdb63 100644 --- a/index.js +++ b/index.js @@ -88,21 +88,22 @@ btns.forEach((btn) => { screen.innerText = screen.innerText / 100; } - if (buttonValue === "DE") { - deteLastEntry(); - } + deteLastEntry(buttonValue); }); }); // end of forEach() statement -function deteLastEntry() { - let newArray = data.slice(0, -1); - screen.innerText = newArray.join(""); - data = newArray; - if (screen.innerText === "") { - screen.innerText = 0; +function deteLastEntry(button) { + if(button === "DE") { + let newArray = data.slice(0, -1); + screen.innerText = newArray.join(""); + data = newArray; + if (screen.innerText === "") { + screen.innerText = 0; + } } + } function canUserAddDot(button) { From d54a98816c9ac9bc571069d3de5e939b6542a862 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Tue, 19 Dec 2023 18:26:27 +0000 Subject: [PATCH 11/19] feat(JS) create convertToPercentage() --- index.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index afbdb63..9b78e1e 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,10 @@ btns.forEach((btn) => { handleNumberButton(buttonValue); + deteLastEntry(buttonValue); + + convertToPercentage(buttonValue); + function userClicksOnEqualButton(button) { if (button === "=") { try { @@ -84,18 +88,22 @@ btns.forEach((btn) => { array.push(outcome); } - if (buttonValue === "%") { - screen.innerText = screen.innerText / 100; - } + - deteLastEntry(buttonValue); + }); }); // end of forEach() statement +function convertToPercentage(button) { + if (button === "%") { + screen.innerText = screen.innerText / 100; + } +} + function deteLastEntry(button) { - if(button === "DE") { + if (button === "DE") { let newArray = data.slice(0, -1); screen.innerText = newArray.join(""); data = newArray; @@ -103,7 +111,6 @@ function deteLastEntry(button) { screen.innerText = 0; } } - } function canUserAddDot(button) { From b5537af02055034d3889e1f980fa9ac9a340f69c Mon Sep 17 00:00:00 2001 From: josueJURE Date: Tue, 19 Dec 2023 18:38:11 +0000 Subject: [PATCH 12/19] feat(JS) stroe following regex /[\/*\-+]/ into a re-usable variable --- index.js | 131 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 71 insertions(+), 60 deletions(-) diff --git a/index.js b/index.js index 9b78e1e..0f14e2a 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,12 @@ const btns = document.querySelectorAll("[data-value]"); let screen = document.querySelector("[data-screen]"); const operators = document.querySelectorAll("[data-operator]"); +const operatorRegex = /[\/*\-+]/; + + + + + let data = []; @@ -30,72 +36,15 @@ btns.forEach((btn) => { convertToPercentage(buttonValue); - function userClicksOnEqualButton(button) { - if (button === "=") { - try { - 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 { - let result = eval(replacedArray.join("")); - console.log(result); - displayResult(replacedArray, 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`; - } - } - } + userClicksOnEqualButton(buttonValue) - 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 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); - } - - - - }); }); // end of forEach() statement +// functions creations + function convertToPercentage(button) { if (button === "%") { screen.innerText = screen.innerText / 100; @@ -224,3 +173,65 @@ function handleNumberButton(button) { screen.innerText = data.join(""); } } + + +////// + + +function userClicksOnEqualButton(button) { + if (button === "=") { + try { + 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 { + let result = eval(replacedArray.join("")); + console.log(result); + displayResult(replacedArray, 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 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 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); +} From ef463c7889118960e916b3ca44384ae940946933 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Tue, 19 Dec 2023 18:40:44 +0000 Subject: [PATCH 13/19] feat(JS) reuse operatorRegex variable in multiple places in code-base --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 0f14e2a..5179dee 100644 --- a/index.js +++ b/index.js @@ -70,14 +70,14 @@ function canUserAddDot(button) { if (data[i] === ".") { dotAllowed = false; break; - } else if (/[\/*\-+]/.test(data[i])) { + } else if (operatorRegex.test(data[i])) { break; } } if (dotAllowed) { if (data.length == 0) { data.push("0"); - } else if (/[\/*\-+]/.test(data[data.length - 1])) { + } else if (operatorRegex.test(data[data.length - 1])) { data.push("0"); } data.push("."); @@ -130,7 +130,7 @@ function insertOpeningParenthesis(button) { isOpenparenthesis = false; break; } - if (/[\/*\-+]/.test(data[i])) { + if (operatorRegex.test(data[i])) { break; } } @@ -155,7 +155,7 @@ function handlingZeroFollowedByAdecimal(button) { } function removesDecimalPointIfPrecededByAnOperator(button) { - if (/[\/*\-+]/.test(button)) { + if (operatorRegex.test(button)) { if (data.slice(-1)[0] === ".") { data.pop(); } From 930eb80c9fddf2563c7125ab900d1695c0775389 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Wed, 20 Dec 2023 15:48:23 +0000 Subject: [PATCH 14/19] feat(JS) Ensure consistency in parameter names --- index.js | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 5179dee..7fceddf 100644 --- a/index.js +++ b/index.js @@ -3,11 +3,6 @@ let screen = document.querySelector("[data-screen]"); const operators = document.querySelectorAll("[data-operator]"); const operatorRegex = /[\/*\-+]/; - - - - - let data = []; btns.forEach((btn) => { @@ -36,15 +31,10 @@ btns.forEach((btn) => { convertToPercentage(buttonValue); - userClicksOnEqualButton(buttonValue) - + userClicksOnEqualButton(buttonValue); }); }); - -// end of forEach() statement - -// functions creations - +// forEach ends & functions creations begins function convertToPercentage(button) { if (button === "%") { screen.innerText = screen.innerText / 100; @@ -150,7 +140,7 @@ function insertClosingParenthesis(button) { function handlingZeroFollowedByAdecimal(button) { if (Number(button) === 0 && screen.innerText.startsWith("0.")) { - screen.innerText += buttonValue; + screen.innerText += button; } } @@ -174,10 +164,6 @@ function handleNumberButton(button) { } } - -////// - - function userClicksOnEqualButton(button) { if (button === "=") { try { @@ -196,9 +182,7 @@ function userClicksOnEqualButton(button) { console.log(result); displayResult(replacedArray, result); screen.innerText = - result === Infinity - ? "You cannot divide by zero. Press AC" - : result; + result === Infinity ? "You cannot divide by zero. Press AC" : result; // divideByZero(screen, result); data = []; data.push(result); @@ -235,3 +219,4 @@ function displayResult(array, outcome) { array = []; array.push(outcome); } +// functions creations ends From 423aee00ec3855337be8f6ebd50e17ad7328a0fa Mon Sep 17 00:00:00 2001 From: josueJURE Date: Wed, 20 Dec 2023 15:50:34 +0000 Subject: [PATCH 15/19] feat(JS) delete one userClicksOnEqualButton(). Called twice --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 7fceddf..53aa31f 100644 --- a/index.js +++ b/index.js @@ -31,7 +31,7 @@ btns.forEach((btn) => { convertToPercentage(buttonValue); - userClicksOnEqualButton(buttonValue); + }); }); // forEach ends & functions creations begins From 572da3dc81285eb83656120e354404f1178750f7 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Wed, 20 Dec 2023 16:08:02 +0000 Subject: [PATCH 16/19] improve readability by creating ZERO & ZERO_DOT constant --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 53aa31f..dbe43f4 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,8 @@ const btns = document.querySelectorAll("[data-value]"); let screen = document.querySelector("[data-screen]"); const operators = document.querySelectorAll("[data-operator]"); const operatorRegex = /[\/*\-+]/; +const ZERO = 0; +const ZERO_DOT = '0.'; let data = []; @@ -139,7 +141,7 @@ function insertClosingParenthesis(button) { } function handlingZeroFollowedByAdecimal(button) { - if (Number(button) === 0 && screen.innerText.startsWith("0.")) { + if (Number(button) === ZERO && screen.innerText.startsWith(ZERO_DOT)) { screen.innerText += button; } } From 73bccb9b7730e851f0bc09783a3605723e8e9749 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Wed, 20 Dec 2023 16:21:01 +0000 Subject: [PATCH 17/19] feat(JS): replace data.join(' ') by data.join('') as this was adding white space --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index dbe43f4..735b6bc 100644 --- a/index.js +++ b/index.js @@ -74,7 +74,7 @@ function canUserAddDot(button) { } data.push("."); } - screen.innerText = data.join(" "); + screen.innerText = data.join(""); } } From c504132c2efcca9c1e5cd2181e8df49f42cb2f21 Mon Sep 17 00:00:00 2001 From: josueJURE Date: Wed, 20 Dec 2023 16:27:34 +0000 Subject: [PATCH 18/19] replace 0 by the constant ZER0 --- index.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 735b6bc..dbfd511 100644 --- a/index.js +++ b/index.js @@ -45,11 +45,11 @@ function convertToPercentage(button) { function deteLastEntry(button) { if (button === "DE") { - let newArray = data.slice(0, -1); + let newArray = data.slice(ZERO, -1); screen.innerText = newArray.join(""); data = newArray; if (screen.innerText === "") { - screen.innerText = 0; + screen.innerText = ZERO; } } } @@ -57,7 +57,7 @@ function deteLastEntry(button) { function canUserAddDot(button) { if (button === ".") { var dotAllowed = true; - for (var i = data.length - 1; i >= 0; i--) { + for (var i = data.length - 1; i >= ZERO; i--) { console.log("data > " + data[i]); if (data[i] === ".") { dotAllowed = false; @@ -67,7 +67,7 @@ function canUserAddDot(button) { } } if (dotAllowed) { - if (data.length == 0) { + if (data.length == ZERO) { data.push("0"); } else if (operatorRegex.test(data[data.length - 1])) { data.push("0"); @@ -82,7 +82,7 @@ function deleteEverythingFromScreen(button) { if (button === "AC") { screen.innerText = ""; data = []; - screen.innerText = 0; + screen.innerText = ZERO; } } @@ -94,15 +94,15 @@ function toggleSign(button) { // debugger if (match) { - let start = currentExpression.length - match[0].length; + let start = currentExpression.length - match[ZERO].length; let end = currentExpression.length; - let currentValue = Number(match[0]); + let currentValue = Number(match[ZERO]); if (!isNaN(currentValue)) { // If it's a number, toggle its sign currentValue = -currentValue; data = data - .slice(0, start) + .slice(ZERO, start) .concat(currentValue.toString().split(""), data.slice(end)); screen.innerText = data.join(""); } @@ -113,7 +113,7 @@ function toggleSign(button) { function insertOpeningParenthesis(button) { if (button === "(") { let isOpenparenthesis = true; - for (let i = data.length - 1; i >= 0; i--) { + for (let i = data.length - 1; i >= ZERO; i--) { if (/^\d$/.test(data[i])) { isOpenparenthesis = false; break; @@ -148,7 +148,7 @@ function handlingZeroFollowedByAdecimal(button) { function removesDecimalPointIfPrecededByAnOperator(button) { if (operatorRegex.test(button)) { - if (data.slice(-1)[0] === ".") { + if (data.slice(-1)[ZERO] === ".") { data.pop(); } button === "*" ? (button = "x") : button === "/" ? (button = "÷") : button; @@ -196,7 +196,7 @@ function userClicksOnEqualButton(button) { } function areYouDivindingByZero(array) { - for (let i = 0; i < array.length - 2; i++) { + for (let i = ZERO; i < array.length - 2; i++) { if ( !isNaN(Number(array[i])) && array[i + 1] === "/" && @@ -209,7 +209,7 @@ function areYouDivindingByZero(array) { } function areYouDividingdZeroByZero(array) { - for (let i = 0; i < array.length - 2; i++) { + for (let i = ZERO; i < array.length - 2; i++) { if (array[i] === "0" && array[i + 1] === "/" && array[i + 2] === "0") { return true; } From 4428f0cfbcdd103e479d4b2d612ccab16867e65c Mon Sep 17 00:00:00 2001 From: josueJURE Date: Fri, 22 Dec 2023 13:14:50 +0000 Subject: [PATCH 19/19] feat(JS): use Number.isFinite() to check that a value is neither Infinity or -Infinity --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index dbfd511..e51b181 100644 --- a/index.js +++ b/index.js @@ -183,8 +183,7 @@ function userClicksOnEqualButton(button) { let result = eval(replacedArray.join("")); console.log(result); displayResult(replacedArray, result); - screen.innerText = - result === Infinity ? "You cannot divide by zero. Press AC" : result; + screen.innerText = !Number.isFinite(result) ? "You cannot divide by zero. Press AC" : result; // divideByZero(screen, result); data = []; data.push(result);