Files
kulakpos_web/public/assets/js/custom/calculator.js

81 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
document.addEventListener("DOMContentLoaded", function () {
const display = document.getElementById("display");
let currentExpression = "";
function buttonClickHandler(event) {
const buttonValue = event.target.innerText;
if (buttonValue === "C") {
currentExpression = "";
}
else if (buttonValue === "⌫") {
currentExpression = currentExpression.slice(0, -1);
}
else if (buttonValue === "=") {
try {
currentExpression = calculate(currentExpression).toString();
} catch {
currentExpression = "Error";
}
}
else if (buttonValue === "√") {
if (!currentExpression) {
currentExpression = "Error";
} else {
currentExpression = Math.sqrt(parseFloat(currentExpression)).toString();
}
}
else if (buttonValue === "x²") {
if (!currentExpression) {
currentExpression = "Error";
} else {
currentExpression = Math.pow(parseFloat(currentExpression), 2).toString();
}
}
else {
if (currentExpression === "Error") {
currentExpression = "";
}
currentExpression += buttonValue;
}
display.value = currentExpression;
}
function calculate(expression) {
expression = expression
.replace(/×/g, "*")
.replace(/÷/g, "/")
.replace(//g, "-");
try {
const result = Function(`return (${expression})`)();
if (result === Infinity || result === -Infinity || isNaN(result)) {
throw new Error("Math Error");
}
return result;
} catch {
return "Error";
}
}
const calculatorButtons = document.querySelectorAll(".button-row button");
calculatorButtons.forEach(button => {
button.addEventListener("click", buttonClickHandler);
});
});