diff --git a/main.js b/main.js index 6fb0fe5..b35eddd 100644 --- a/main.js +++ b/main.js @@ -11,56 +11,60 @@ let decimalAdded = false; const operators = ["+", "-", "x", "÷"]; -function handleKeyPress (e) { +function handleKeyPress(e) { const key = e.target.dataset.key; const lastChar = operation[operation.length - 1]; - if (key === "=") { - return; - } + // Prevent action if the key is "=" + if (key === "=") return; - if (key === "." && decimalAdded) { - return; - } + // Prevent multiple decimals in a number + if (key === "." && decimalAdded) return; - if (operators.indexOf(key) !== -1) { + // Reset decimalAdded flag when an operator is pressed + if (operators.includes(key)) { decimalAdded = false; } + // Allow leading minus sign for negative numbers if (operation.length === 0 && key === "-") { operation += key; input.innerHTML = operation; return; } - if (operation.length === 0 && operators.indexOf(key) !== -1) { + // Prevent leading operators other than minus + if (operation.length === 0 && operators.includes(key)) { input.innerHTML = operation; return; } - if (operators.indexOf(lastChar) !== -1 && operators.indexOf(key) !== -1) { - operation = operation.replace(/.$/, key); + // Replace the last operator with the new one if two operators are pressed consecutively + if (operators.includes(lastChar) && operators.includes(key)) { + operation = operation.slice(0, -1) + key; input.innerHTML = operation; return; } + // Add the pressed key to the operation if (key) { if (key === ".") decimalAdded = true; operation += key; input.innerHTML = operation; return; } - } -function evaluate(e) { +function evaluateExpression(e) { const key = e.target.dataset.key; const lastChar = operation[operation.length - 1]; - if (key === "=" && operators.indexOf(lastChar) !== -1) { + // Remove the last operator if "=" is pressed + if (key === "=" && operators.includes(lastChar)) { operation = operation.slice(0, -1); } + // Display empty result if no operation is present if (operation.length === 0) { answer = ""; result.innerHTML = answer; @@ -68,13 +72,14 @@ function evaluate(e) { } try { - + // Remove leading zero unless it's a decimal number if (operation[0] === "0" && operation[1] !== "." && operation.length > 1) { operation = operation.slice(1); } - const final = operation.replace(/x/g, "*").replace(/÷/g, "/"); - answer = +(eval(final)).toFixed(5); + // Replace custom operators with JavaScript operators and evaluate + const finalExpression = operation.replace(/x/g, "*").replace(/÷/g, "/"); + answer = +(eval(finalExpression)).toFixed(5); if (key === "=") { decimalAdded = false; @@ -86,20 +91,18 @@ function evaluate(e) { } result.innerHTML = answer; - - } catch (e) { + } catch (error) { if (key === "=") { decimalAdded = false; input.innerHTML = `${operation}`; result.innerHTML = `Bad Expression`; } - console.log(e); + console.error(error); } - } -function clearInput (e) { - +function clearInput(e) { + // Clear all input if Ctrl key is pressed if (e.ctrlKey) { operation = ""; answer = ""; @@ -108,13 +111,14 @@ function clearInput (e) { return; } + // Remove the last character from the operation operation = operation.slice(0, -1); input.innerHTML = operation; - } +// Add event listeners deleteBtn.addEventListener("click", clearInput); keys.forEach(key => { key.addEventListener("click", handleKeyPress); - key.addEventListener("click", evaluate); + key.addEventListener("click", evaluateExpression); });