From d690ea5612e65ca7670c9c14781fa71f189517e1 Mon Sep 17 00:00:00 2001 From: stephen Brooks Date: Mon, 26 Jun 2023 23:10:46 -0400 Subject: [PATCH 1/2] adding functions --- public/calculator.html | 11 ++++++----- src/calculator.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/public/calculator.html b/public/calculator.html index 65321f9..ea10381 100644 --- a/public/calculator.html +++ b/public/calculator.html @@ -7,6 +7,7 @@ +
@@ -27,7 +28,7 @@
-
+
@@ -45,10 +46,10 @@
-
-
-
-
+
+
+
+
diff --git a/src/calculator.js b/src/calculator.js index e69de29..de42cec 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -0,0 +1,35 @@ +window.addEventListener('load', (event) => { + //I am seccond guessing initializing here + if (!sessionStorage.getItem('calculatorMemory')){ + //store each part of the expression + sessionStorage.setItem('calculatorMemory', { + statementIndex: 0, + statement: [], + operands: ['+', '-', '/', '*', '(', ')'] + }); + // example storage: + // {statementIndex:#, statement: [111, '+', 222, '*', '(', 333, '/', 444, ')'], ? } + } + if (!sessionStorage.getItem('calculatorMemoryHistory')){ + //store calculatorMemory history - limit to 5? + sessionStorage.setItem('calculatorMemoryHistory', {}) + } + //I can see how React would thrive here lol + +}); + +const addNumeral = function(numeral){ + //insertNumeralIntocalculatorMemory() + //showcalculatorMemory(); + const {statementIndex, statement, operands} = sessionStorage.getItem('calculatorMemory') + const lastIndex = statement.length - 1 + const lastItem = statement.pop(); + operands.includes(lastItem) ? 1 : 2; //where was I going here? + typeof(lastItem) == typeof(numeral) ? statement[lastIndex].concat(numeral) : statement.push(numeral) + + +}; + +const addOperand = function(operand){ + console.log(operand) +}; \ No newline at end of file From 36febe7da4ddcadb144a75c7d3ebd8c599fdc883 Mon Sep 17 00:00:00 2001 From: Stephen Brooks Date: Sat, 8 Jul 2023 23:52:40 -0400 Subject: [PATCH 2/2] logic for buttons but issue with setItem, ln6 --- public/calculator.html | 2 +- src/calculator.css | 4 ++- src/calculator.js | 73 +++++++++++++++++++++++++++++++++--------- 3 files changed, 61 insertions(+), 18 deletions(-) diff --git a/public/calculator.html b/public/calculator.html index ea10381..790d11e 100644 --- a/public/calculator.html +++ b/public/calculator.html @@ -49,7 +49,7 @@
-
+
diff --git a/src/calculator.css b/src/calculator.css index ac28bc6..62fc11b 100644 --- a/src/calculator.css +++ b/src/calculator.css @@ -17,6 +17,8 @@ body { flex-direction: column; background: black; height: 85vh; + max-height: 750px; + min-height: 475px; width: 25vw; min-width: 20vw; margin-top: 1vh; @@ -53,7 +55,7 @@ body { } .buttons button { - height:50px; + height: 50px; width: 50px; border: 1px solid; border-radius: 50%; diff --git a/src/calculator.js b/src/calculator.js index de42cec..0bcd9b3 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -1,35 +1,76 @@ window.addEventListener('load', (event) => { - //I am seccond guessing initializing here + if(sessionStorage.getItem('calculatorMemory')){ + sessionStorage.removeItem('calculatorMemory') + } if (!sessionStorage.getItem('calculatorMemory')){ - //store each part of the expression sessionStorage.setItem('calculatorMemory', { - statementIndex: 0, + last: null, + lastIndex: 0, statement: [], - operands: ['+', '-', '/', '*', '(', ')'] + operands: ['+', '-', '/', '*', '(', ')', '^'] }); // example storage: // {statementIndex:#, statement: [111, '+', 222, '*', '(', 333, '/', 444, ')'], ? } + //there may be an easier way to do this? } if (!sessionStorage.getItem('calculatorMemoryHistory')){ //store calculatorMemory history - limit to 5? sessionStorage.setItem('calculatorMemoryHistory', {}) } //I can see how React would thrive here lol - }); -const addNumeral = function(numeral){ - //insertNumeralIntocalculatorMemory() - //showcalculatorMemory(); - const {statementIndex, statement, operands} = sessionStorage.getItem('calculatorMemory') - const lastIndex = statement.length - 1 - const lastItem = statement.pop(); - operands.includes(lastItem) ? 1 : 2; //where was I going here? - typeof(lastItem) == typeof(numeral) ? statement[lastIndex].concat(numeral) : statement.push(numeral) - - +const addNumeral = function(x){ + const {last, lastIndex, statement, operands} = sessionStorage.getItem('calculatorMemory'); + if (typeof(last) == 'number' && typeof(x) == 'number'){ + last = Number([statement[lastIndex], x].join('')); + statement[lastIndex] = last; + } else if (operands.includes(last) && typeof(x) == 'number'){ + statement.push(x); + last = x; + lastIndex = statement.length - 1; + } else if (operands.includes(last) && operands.includes(x)){ + statement[lastIndex] = x + last = x; + //lastIndex stays the same + } + sessionStorage.setItem('calculatorMemory', { + last, + lastIndex, + statement, + operands + }) + let expressionDisplay = document.getElementsByClassName("expression") + expressionDisplay.innerText = statement.join('') }; const addOperand = function(operand){ console.log(operand) -}; \ No newline at end of file +}; + +//Pseudocode first + +/* +//button clicked 'x' (for most buttons) +if last item and x are both numbers + concat last item and x in memory + set concat as last + +if last item is an operator and x is a number + push x into statement +if last item and x are both operators + x replaces last item +end +set session storage +update display + +//button clicked 'equals' --recursive oppertunity +//order of opperations -carefuly + does statement contain a '(', if so, and more than one, find last + --assuming closing ')' at end if none supplied, maybe add them in? + calculate inside +*/ + + +// -- documentation +//store last 'operation' \ No newline at end of file