From d8cb62601431a8dfb5e5cc8e6d0e03a3784a1f60 Mon Sep 17 00:00:00 2001 From: jfgiraud Date: Fri, 21 Aug 2026 11:42:22 +0200 Subject: [PATCH] calculatrice js V1.0 --- README.md | 0 calculatrice.js | 146 ++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 26 +++++++++ 3 files changed, 172 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/calculatrice.js b/calculatrice.js index e69de29..38ea3d4 100644 --- a/calculatrice.js +++ b/calculatrice.js @@ -0,0 +1,146 @@ +class SimpleCalculator { + constructor(containerId) { + this.container = document.getElementById(containerId); + if (!this.container) return; + this.currentInput = "0"; + this.previousInput = ""; + this.operation = null; + this.init(); + } + + init() { + this.container.innerHTML = ` +
+ +
+
+
0
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + +
+
+ `; + + this.prevScreen = this.container.querySelector("#calc-prev"); + this.currentScreen = this.container.querySelector("#calc-current"); + this.addListeners(); + } + + addListeners() { + this.container.addEventListener("click", (e) => { + const button = e.target.closest("button"); + if (!button) return; + + const action = button.getAttribute("data-action"); + const value = button.getAttribute("data-value"); + + if (action === "number") { + this.appendNumber(value); + } else if (action === "operator") { + this.chooseOperation(value); + } else if (action === "calculate") { + this.compute(); + } else if (action === "clear") { + this.clear(); + } else if (action === "delete") { + this.deleteDigit(); + } + + this.updateScreen(); + }); + } + + appendNumber(number) { + if (number === "." && this.currentInput.includes(".")) return; + if (this.currentInput === "0" && number !== ".") { + this.currentInput = number; + } else { + this.currentInput += number; + } + } + + chooseOperation(op) { + if (this.currentInput === "") return; + if (this.previousInput !== "") { + this.compute(); + } + this.operation = op; + this.previousInput = this.currentInput; + this.currentInput = ""; + } + + compute() { + let computation; + const prev = parseFloat(this.previousInput); + const current = parseFloat(this.currentInput); + if (isNaN(prev) || isNaN(current)) return; + + switch (this.operation) { + case "+": + computation = prev + current; + break; + case "-": + computation = prev - current; + break; + case "*": + computation = prev * current; + break; + case "/": + computation = current === 0 ? "Erreur" : prev / current; + break; + default: + return; + } + + this.currentInput = computation.toString(); + this.operation = null; + this.previousInput = ""; + } + + clear() { + this.currentInput = "0"; + this.previousInput = ""; + this.operation = null; + } + + deleteDigit() { + if (this.currentInput.length === 1) { + this.currentInput = "0"; + } else { + this.currentInput = this.currentInput.slice(0, -1); + } + } + + updateScreen() { + this.currentScreen.innerText = + this.currentInput === "" ? "0" : this.currentInput; + if (this.operation != null) { + this.prevScreen.innerText = `${this.previousInput} ${this.operation}`; + } else { + this.prevScreen.innerText = ""; + } + } +} diff --git a/index.html b/index.html index e69de29..813edaf 100644 --- a/index.html +++ b/index.html @@ -0,0 +1,26 @@ + + + + + + Calculatrice Autonome + + + + + +
+ + + + + + + +