diff --git a/index.js b/index.js index e6e24e4..e51b181 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,9 @@ 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 = []; @@ -8,186 +11,213 @@ 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(buttonValue); - if (buttonValue === ")") { - data.push(")"); - screen.innerText = data.join(""); - } + insertClosingParenthesis(buttonValue); - if (buttonValue === "AC") { - deleteEverythingFromScreen(); - } + deleteEverythingFromScreen(buttonValue); - if (Number(buttonValue) === 0 && screen.innerText.startsWith("0.")) { - screen.innerText += buttonValue; - } - if (!isNaN(Number(buttonValue))) { - screen.innerText = buttonValue; - data.push(screen.innerText); - screen.innerText = data.join(""); + toggleSign(buttonValue); + + canUserAddDot(buttonValue); + + userClicksOnEqualButton(buttonValue); + + handlingZeroFollowedByAdecimal(buttonValue); + + removesDecimalPointIfPrecededByAnOperator(buttonValue); + + handleNumberButton(buttonValue); + + deteLastEntry(buttonValue); + + convertToPercentage(buttonValue); + + + }); +}); +// forEach ends & functions creations begins +function convertToPercentage(button) { + if (button === "%") { + screen.innerText = screen.innerText / 100; + } +} + +function deteLastEntry(button) { + if (button === "DE") { + let newArray = data.slice(ZERO, -1); + screen.innerText = newArray.join(""); + data = newArray; + if (screen.innerText === "") { + screen.innerText = ZERO; } - if (/[\/*\-+]/.test(buttonValue)) { - if (data.slice(-1)[0] === ".") { - data.pop(); + } +} + +function canUserAddDot(button) { + if (button === ".") { + var dotAllowed = true; + for (var i = data.length - 1; i >= ZERO; i--) { + console.log("data > " + data[i]); + if (data[i] === ".") { + dotAllowed = false; + break; + } else if (operatorRegex.test(data[i])) { + break; } - buttonValue === "*" - ? (buttonValue = "x") - : buttonValue === "/" - ? (buttonValue = "÷") - : buttonValue; - - data.push(buttonValue); - screen.innerText = data.join(""); } - if (buttonValue === "minus") { - toggleSign(); + if (dotAllowed) { + if (data.length == ZERO) { + data.push("0"); + } else if (operatorRegex.test(data[data.length - 1])) { + data.push("0"); + } + data.push("."); } + screen.innerText = data.join(""); + } +} - if (buttonValue === ".") { - canUserAddDot(); - } +function deleteEverythingFromScreen(button) { + if (button === "AC") { + screen.innerText = ""; + data = []; + screen.innerText = ZERO; + } +} - 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 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[ZERO].length; + let end = currentExpression.length; + let currentValue = Number(match[ZERO]); + + if (!isNaN(currentValue)) { + // If it's a number, toggle its sign + currentValue = -currentValue; + data = data + .slice(ZERO, start) + .concat(currentValue.toString().split(""), data.slice(end)); + screen.innerText = data.join(""); } } + } +} - 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; - } +function insertOpeningParenthesis(button) { + if (button === "(") { + let isOpenparenthesis = true; + for (let i = data.length - 1; i >= ZERO; i--) { + if (/^\d$/.test(data[i])) { + isOpenparenthesis = false; + break; } - 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; - } + if (data[i] === ")") { + isOpenparenthesis = false; + break; + } + if (operatorRegex.test(data[i])) { + break; } - return false; } - - function displayResult(array, outcome) { - array = []; - array.push(outcome); + if (isOpenparenthesis) { + data.push("("); } + screen.innerText = data.join(""); + } +} - if (buttonValue === "%") { - screen.innerText = screen.innerText / 100; - } +function insertClosingParenthesis(button) { + if (button === ")") { + data.push(")"); + screen.innerText = data.join(""); + } +} - if (buttonValue === "DE") { - deteLastEntry(); +function handlingZeroFollowedByAdecimal(button) { + if (Number(button) === ZERO && screen.innerText.startsWith(ZERO_DOT)) { + screen.innerText += button; + } +} + +function removesDecimalPointIfPrecededByAnOperator(button) { + if (operatorRegex.test(button)) { + if (data.slice(-1)[ZERO] === ".") { + data.pop(); } - }); -}); + button === "*" ? (button = "x") : button === "/" ? (button = "÷") : button; -function deteLastEntry() { - let newArray = data.slice(0, -1); - screen.innerText = newArray.join(""); - data = newArray; - if (screen.innerText === "") { - screen.innerText = 0; + data.push(button); + screen.innerText = data.join(""); } } -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 handleNumberButton(button) { + if (!isNaN(Number(button))) { + screen.innerText = button; + data.push(screen.innerText); + screen.innerText = data.join(""); } - if (dotAllowed) { - if (data.length == 0) { - data.push("0"); - } else if (/[\/*\-+]/.test(data[data.length - 1])) { - data.push("0"); +} + +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 = !Number.isFinite(result) ? "You cannot divide by zero. Press AC" : result; + // divideByZero(screen, result); + data = []; + data.push(result); + } + } catch (e) { + screen.innerText = `${e.name} press AC`; } - data.push("."); } - screen.innerText = data.join(" "); } -function deleteEverythingFromScreen() { - screen.innerText = ""; - data = []; - screen.innerText = 0; +function areYouDivindingByZero(array) { + for (let i = ZERO; i < array.length - 2; i++) { + if ( + !isNaN(Number(array[i])) && + array[i + 1] === "/" && + array[i + 2] === "0" + ) { + return true; + } + } + return false; } -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 areYouDividingdZeroByZero(array) { + for (let i = ZERO; 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); } +// functions creations ends