Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions public/calculator.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="stylesheet" href="../src/calculator.css">
<script src="../src/calculator.js"></script>
</head>
<body>
<body>
<div class="calc">
<div class="display">
Expand All @@ -27,7 +28,7 @@
<div><button>inv</button></div>

<div><button>!</button></div>
<div><button>C</button></div>
<div><button>C</button></div> <!-- sessionStorage.clear(); -->
<div><button>%</button></div>
<div><button>del</button></div>
<div><button>/</button></div>
Expand All @@ -45,10 +46,10 @@
<div><button>-</button></div>

<div><button>pi</button></div>
<div><button>1</button></div>
<div><button>2</button></div>
<div><button>3</button></div>
<div><button>+</button></div>
<div><button onclick="addNumeral(1)">1</button></div>
<div><button onclick="">2</button></div>
<div><button onclick="">3</button></div>
<div><button onclick="addNumeral('+')">+</button></div>

<div><button>e</button></div>
<div><button>00</button></div>
Expand Down
4 changes: 3 additions & 1 deletion src/calculator.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,7 +55,7 @@ body {
}

.buttons button {
height:50px;
height: 50px;
width: 50px;
border: 1px solid;
border-radius: 50%;
Expand Down
76 changes: 76 additions & 0 deletions src/calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
window.addEventListener('load', (event) => {
if(sessionStorage.getItem('calculatorMemory')){
sessionStorage.removeItem('calculatorMemory')
}
if (!sessionStorage.getItem('calculatorMemory')){
sessionStorage.setItem('calculatorMemory', {
last: null,
lastIndex: 0,
statement: [],
operands: ['+', '-', '/', '*', '(', ')', '^']
});
// example storage:
// {statementIndex:#<if this is even needed?>, 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(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)
};

//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'