Skip to content

Commit

Permalink
Implement unary operator buttons + tidying up
Browse files Browse the repository at this point in the history
Currently only square root available with current button layout but
should now be extensible to other unary functions. There is a bug where
applying a unary function allows editing the number, but changing this
by setting newNum to true prevents performing other operations with the
results of unary operations.
  • Loading branch information
strbytes committed Apr 25, 2022
1 parent 91e2108 commit 4cb563a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class="button-row">
<button id="C" class="clear">C</button>
<button id="CE" class="clear">CE</button>
<button id="root" class="unaryOperators"></button>
<button id="sqrt" class="unaryOperators"></button>
<button id="div" class="binaryOperators">/</button>
</div>
<div class="button-row">
Expand Down
26 changes: 19 additions & 7 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const Calculator = {
};
},
applyUnaryOperation: function (operator, x) {
Screen.newNum = true;
Screen.display(UnaryOperators[operator](x));
},

Expand All @@ -35,6 +34,11 @@ const Calculator = {
}
Calculator.currOperation = null;
},

clear: function () {
Calculator.currOperation = null;
Calculator.prevOperation = null;
},
};

// Store operations to be used by the calculator
Expand Down Expand Up @@ -141,10 +145,22 @@ function binaryOperatorButtonPress(event) {
// operations if another operator button is pressed
Calculator.execute(num);
}
Calculator.newBinaryOperation(operator, +Screen.screenSelector.textContent);
Calculator.newBinaryOperation(operator, num);
Screen.newNum = true;
}

const unaryOperatorButtons = document.querySelector(".unaryOperators");
unaryOperatorButtons.addEventListener("click", unaryOperatorButtonPress);
function unaryOperatorButtonPress(event) {
if (Screen.screenSelector.textContent.includes("ERR"))
return; // do nothing if error
else if (Screen.newNum) Calculator.clear();
let operator = event.target.id;
num = +Screen.screenSelector.textContent;
Screen.display(UnaryOperators[operator](num));
// Screen.newNum = true;
}

const equalsButton = document.querySelector("#equals");
equalsButton.addEventListener("click", equalsButtonPress);
function equalsButtonPress(_) {
Expand All @@ -161,14 +177,10 @@ function clearButtonPress(event) {
Screen.screenSelector.textContent = "0";
if (event.target.id == "C") {
// Clear all
Calculator.currOperation = null;
Calculator.prevOperation = null;
Calculator.clear();
}
}

const unaryOperatorButtons = document.querySelector("#root");
unaryOperatorButtons.querySelectorAll(".unaryOperators");

const signButton = document.querySelector("#sign");
const decimalButton = document.querySelector("#decimal");

Expand Down

0 comments on commit 4cb563a

Please sign in to comment.