mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-26 22:30:44 -05:00
feat: updates 1st/2nd nums
This commit is contained in:
parent
56e16c3741
commit
c75625be29
2 changed files with 89 additions and 36 deletions
|
@ -15,25 +15,25 @@
|
||||||
0
|
0
|
||||||
</div>
|
</div>
|
||||||
<div class="buttons">
|
<div class="buttons">
|
||||||
<button id="clear" class="btn" value="cls">A/C</button>
|
<button id="clear" class="btn" value="cls" data-clear="">A/C</button>
|
||||||
<button id="action" class="btn" value="+/-">+/-</button>
|
<button id="action" class="btn" value="+/-" data-negate="">+/-</button>
|
||||||
<button id="action" class="btn" value="%">%</button>
|
<button id="action" class="btn" value="%" data-ops="">%</button>
|
||||||
<button id="action" class="btn" value="÷">÷</button>
|
<button id="action" class="btn" value="÷" data-ops="">÷</button>
|
||||||
<button class="btn" value="7">7</button>
|
<button class="btn" value="7" data-num="">7</button>
|
||||||
<button class="btn" value="8">8</button>
|
<button class="btn" value="8" data-num="">8</button>
|
||||||
<button class="btn" value="9">9</button>
|
<button class="btn" value="9" data-num="">9</button>
|
||||||
<button id="action" class="btn" value="*">*</button>
|
<button id="action" class="btn" value="*" data-ops="">*</button>
|
||||||
<button class="btn" value="4">4</button>
|
<button class="btn" value="4" data-num="">4</button>
|
||||||
<button class="btn" value="5">5</button>
|
<button class="btn" value="5" data-num="">5</button>
|
||||||
<button class="btn" value="6">6</button>
|
<button class="btn" value="6" data-num="">6</button>
|
||||||
<button id="action" class="btn" value="-">-</button>
|
<button id="action" class="btn" value="-" data-ops="">-</button>
|
||||||
<button class="btn" value="1">1</button>
|
<button class="btn" value="1" data-num="">1</button>
|
||||||
<button class="btn" value="2">2</button>
|
<button class="btn" value="2" data-num="">2</button>
|
||||||
<button class="btn" value="3">3</button>
|
<button class="btn" value="3" data-num="">3</button>
|
||||||
<button id="action" class="btn" value="+">+</button>
|
<button id="action" class="btn" value="+" data-ops="">+</button>
|
||||||
<button id="zero" class="btn" value="0">0</button>
|
<button id="zero" class="btn" value="0" data-num="">0</button>
|
||||||
<button id="decimal" class="btn" value=".">.</button>
|
<button id="decimal" class="btn" value="." data-num="">.</button>
|
||||||
<button id="action" class="btn" value="=">=</button>
|
<button id="action" class="btn" value="=" data-ops="">=</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<script src="js/script.js"></script>
|
<script src="js/script.js"></script>
|
||||||
|
|
|
@ -1,13 +1,32 @@
|
||||||
let firstNum = undefined;
|
|
||||||
let secondNum = undefined;
|
|
||||||
let operator = undefined;
|
|
||||||
let screen = document.querySelector('.screen');
|
let screen = document.querySelector('.screen');
|
||||||
|
|
||||||
|
|
||||||
const buttons = document.querySelectorAll('.btn');
|
const buttons = document.querySelectorAll('.btn');
|
||||||
|
|
||||||
|
const ops = {
|
||||||
|
firstNum: String(),
|
||||||
|
secondNum: String(),
|
||||||
|
operator: undefined,
|
||||||
|
numFlag: false,
|
||||||
|
|
||||||
|
updateNum: (num) => {
|
||||||
|
if (!this.updateSecondNum){
|
||||||
|
this.firstNum = num;
|
||||||
|
} else {
|
||||||
|
this.secondNum = num;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
toggleNumFlag: function() {
|
||||||
|
this.numFlag = !this.numFlag
|
||||||
|
},
|
||||||
|
clear: function(){
|
||||||
|
this.firstNum = Number();
|
||||||
|
this.secondNum = Number();
|
||||||
|
this.operator = undefined;
|
||||||
|
this.numFlag = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
function add(x, y) {
|
function add(x, y) {
|
||||||
return Number(x + y);
|
return x + y;
|
||||||
}
|
}
|
||||||
|
|
||||||
function subtract(x, y) {
|
function subtract(x, y) {
|
||||||
|
@ -41,21 +60,55 @@ function operate(firstNum, secondNum, operator) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateDisplay(displayValue) {
|
function updateDisplay(displayValue) {
|
||||||
let currentScreen = screen.textContent.trim();
|
|
||||||
console.log(currentScreen);
|
|
||||||
if (currentScreen.length >= 8) return;
|
|
||||||
|
|
||||||
if (Number(currentScreen) === 0) {
|
|
||||||
screen.textContent = displayValue;
|
screen.textContent = displayValue;
|
||||||
} else {
|
|
||||||
screen.textContent += displayValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function calculator(event){
|
||||||
|
|
||||||
|
if(event.target.dataset.ops === "") {
|
||||||
|
// TODO handle ops for = or clear
|
||||||
|
// TODO other ops should indicate a swap to second number
|
||||||
|
ops.operator = event.target.value;
|
||||||
|
//TODO fix this conditional should be if numFlag and a check if = or additional ops
|
||||||
|
if(ops.secondNum){
|
||||||
|
console.log(operate(ops.firstNum, ops.secondNum, ops.operator));
|
||||||
|
console.log(ops.operator);
|
||||||
|
} else {
|
||||||
|
ops.toggleNumFlag();
|
||||||
|
console.log(ops.numFlag);
|
||||||
|
console.log('set second num');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(event.target.dataset.num === "") {
|
||||||
|
if(!ops.numFlag) {
|
||||||
|
ops.firstNum += event.target.value;
|
||||||
|
updateDisplay(ops.firstNum);
|
||||||
|
} else {
|
||||||
|
ops.secondNum += event.target.value;
|
||||||
|
updateDisplay(ops.secondNum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if (currentScreen.length >= 8) return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
buttons.forEach(btn => {
|
buttons.forEach(btn => {
|
||||||
btn.addEventListener('click', e => {
|
btn.addEventListener('click', calculator);
|
||||||
updateDisplay(e.target.value);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
user imputs 1stNumber up to 8 digits or until user presses operator
|
||||||
|
number should be stored. each input evauluating for operator
|
||||||
|
1st number constantly updating
|
||||||
|
operator stored
|
||||||
|
user then enters 2nd Number up to 8 digits or until operator pressed
|
||||||
|
if user presses equals result is stored
|
||||||
|
if user presses operator result is saved to 1st number
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
Loading…
Reference in a new issue