diff --git a/demos/calculator-app/app.js b/demos/calculator-app/app.js new file mode 100644 index 000000000..1fce73256 --- /dev/null +++ b/demos/calculator-app/app.js @@ -0,0 +1,44 @@ +const display = document.getElementById("display"); +const buttons = document.querySelectorAll("button"); + +buttons.forEach(button => { + button.addEventListener("click", () => handleInput(button.innerText)); +}); + +function handleInput(value) { + if (value === "C") { + display.value = ""; + } else if (value === "⌫") { + display.value = display.value.slice(0, -1); + } else if (value === "=") { + calculate(); + } else { + display.value += value; + } +} + +function calculate() { + try { + const result = display.value + .replace(/×/g, "*") + .replace(/÷/g, "/") + .replace(/−/g, "-"); + + display.value = eval(result); + } catch { + display.value = "Error"; + } +} + +/* Keyboard Support */ +document.addEventListener("keydown", (e) => { + if ((e.key >= 0 && e.key <= 9) || "+-*/.".includes(e.key)) { + display.value += e.key; + } else if (e.key === "Enter") { + calculate(); + } else if (e.key === "Backspace") { + display.value = display.value.slice(0, -1); + } else if (e.key === "Escape") { + display.value = ""; + } +}); diff --git a/demos/calculator-app/index.html b/demos/calculator-app/index.html new file mode 100644 index 000000000..3cae93f64 --- /dev/null +++ b/demos/calculator-app/index.html @@ -0,0 +1,41 @@ + + +
+ +