From ea48cdb7c34e628a3b488fc1b39b3f30445a400d Mon Sep 17 00:00:00 2001 From: andrzej Date: Sat, 25 May 2024 13:12:09 +0200 Subject: [PATCH] another change to hopefully trigger webhook --- src/App.js | 1 - src/Components/Calculator.js | 258 ++++++++++++++++++++--------------- 2 files changed, 149 insertions(+), 110 deletions(-) diff --git a/src/App.js b/src/App.js index f577b76..622a81e 100644 --- a/src/App.js +++ b/src/App.js @@ -4,7 +4,6 @@ import Calculator from "./Components/Calculator"; function App() { return (
- I changed something else!
); diff --git a/src/Components/Calculator.js b/src/Components/Calculator.js index c2d61f4..2728db6 100644 --- a/src/Components/Calculator.js +++ b/src/Components/Calculator.js @@ -2,118 +2,158 @@ import { useState } from "react"; import KeyPad from "./KeyPad"; export default function Calculator(props) { - const [main, setMain] = useState("0") - const [memory, setMemory] = useState("") - const [operation, setOperation] = useState("") - const [on, setOn] = useState(true) + const [main, setMain] = useState("0"); + const [memory, setMemory] = useState(""); + const [operation, setOperation] = useState(""); + const [on, setOn] = useState(true); - const map = { - conc: (numberString) => { - if(main.length<=9){ - if (main[0] === "0") { - setMain(prev => prev.slice(1) + numberString) - } else { - setMain(prev => prev + numberString) - } - } - }, - - zero: function () { this.conc("0") }, - one: function () { this.conc("1") }, - two: function () { this.conc("2") }, - three: function () { this.conc("3") }, - four: function () { this.conc("4") }, - five: function () { this.conc("5") }, - six: function () { this.conc("6") }, - seven: function () { this.conc("7") }, - eight: function () { this.conc("8") }, - nine: function () { this.conc("9") }, - decimal: function () { - if (!/\./g.test(main)) { - this.conc(".") - } - }, - - add: function () { this.rcvOperator("add") }, - subtract: function () { this.rcvOperator("subtract") }, - multiply: function () { this.rcvOperator("multiply") }, - divide: function () { this.rcvOperator("divide") }, - - rcvOperator: function (operator) { - if (main === "-") { - setMain("") - } else if (main !== "") { - if (memory === "") { - setMemory(main) - } else { - setMemory(prev => { return this.calculate(main, prev) }) - } - setMain("") - } - if (main === "" && operator === "subtract") { - setMain(prev => { - if (prev[0] !== "-") { - return "-" + prev - } - }) - } else { - setOperation(operator) - } - - - }, - - operations: { - add: (a, b) => { return a + b }, - subtract: (a, b) => { return b - a }, - multiply: (a, b) => { return a * b }, - divide: (a, b) => { return b / a }, - }, - - clear: () => { - setMain("0") - setMemory("") - setOperation("") - }, - equals: function () { - if (operation !== "" && main !== "") { - setMain(prev => { - return this.calculate(prev, memory) - }) - setMemory("") - setOperation("") - } - - }, - on:()=>setOn(true), - off:()=>{ - setOn(false) - setMain("0") - setMemory("") - setOperation("") - }, - calculate: function (a, b) { - const string = this.operations[operation](parseFloat(a), parseFloat(b)).toString() - return string.length<=9?string:string.slice(0,10) + const map = { + conc: (numberString) => { + if (main.length <= 9) { + if (main[0] === "0") { + setMain((prev) => prev.slice(1) + numberString); + } else { + setMain((prev) => prev + numberString); } - } + } + }, - function handleInput(content) { - map[content]() - } + zero: function () { + this.conc("0"); + }, + one: function () { + this.conc("1"); + }, + two: function () { + this.conc("2"); + }, + three: function () { + this.conc("3"); + }, + four: function () { + this.conc("4"); + }, + five: function () { + this.conc("5"); + }, + six: function () { + this.conc("6"); + }, + seven: function () { + this.conc("7"); + }, + eight: function () { + this.conc("8"); + }, + nine: function () { + this.conc("9"); + }, + decimal: function () { + if (!/\./g.test(main)) { + this.conc("."); + } + }, + add: function () { + this.rcvOperator("add"); + }, + subtract: function () { + this.rcvOperator("subtract"); + }, + multiply: function () { + this.rcvOperator("multiply"); + }, + divide: function () { + this.rcvOperator("divide"); + }, - return ( -
-
-

CALCULATOR

-
-

{memory}

-

{main}

-

{operation}

-
- -
+ rcvOperator: function (operator) { + if (main === "-") { + setMain(""); + } else if (main !== "") { + if (memory === "") { + setMemory(main); + } else { + setMemory((prev) => { + return this.calculate(main, prev); + }); + } + setMain(""); + } + if (main === "" && operator === "subtract") { + setMain((prev) => { + if (prev[0] !== "-") { + return "-" + prev; + } + }); + } else { + setOperation(operator); + } + }, + + operations: { + add: (a, b) => { + return a + b; + }, + subtract: (a, b) => { + return b - a; + }, + multiply: (a, b) => { + return a * b; + }, + divide: (a, b) => { + return b / a; + }, + }, + + clear: () => { + setMain("0"); + setMemory(""); + setOperation(""); + }, + equals: function () { + if (operation !== "" && main !== "") { + setMain((prev) => { + return this.calculate(prev, memory); + }); + setMemory(""); + setOperation(""); + } + }, + on: () => setOn(true), + off: () => { + setOn(false); + setMain("0"); + setMemory(""); + setOperation(""); + }, + calculate: function (a, b) { + const string = this.operations[operation]( + parseFloat(a), + parseFloat(b), + ).toString(); + return string.length <= 9 ? string : string.slice(0, 10); + }, + }; + + function handleInput(content) { + map[content](); + } + + return ( +
+
+
+

CALCULATOR - CI/CD EDITION

+
+
+

{memory}

+

{main}

+

{operation}

- ) -} \ No newline at end of file + +
+
+ ); +} +