Skip to content

Commit

Permalink
fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tintintani committed Aug 5, 2021
1 parent 3d88f66 commit a5daf6f
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const calculator = {
};

getNumber = (num) => {
if (calculator.secondOperand == null && calculator.firstOperand == null) text = "";
if (text == "0") text = "";
if (equaled && calculator.secondOperand == null) text = "";

if (calculatorReset) {
document.getElementById("outputTopLine").style.textAlign = "right";
document.getElementById("outputTopLine").innerHTML = null;
Expand All @@ -15,11 +17,14 @@ getNumber = (num) => {
calculatorReset = false;
}

equaled = false;

operatorPlaced = false;
text += num.value;
Operand += num.value;
document.getElementById("outputTopLine").innerHTML = text;
secondOperandTurn
? ((calculator.secondOperand = parseFloat(Operand)), (operatorPlaced = false))
? ((calculator.secondOperand = parseFloat(Operand)))
: (calculator.firstOperand = parseFloat(Operand));
console.log(calculator.firstOperand + "," + calculator.secondOperand);
console.log("text = " + text);
Expand All @@ -28,19 +33,20 @@ getNumber = (num) => {
};

setOperator = (op) => {
if ((op.value == "-" && calculatorReset) || operatorTurn) {
if (firstMinusSign || operatorTurn) {
if (equaled) equaled = false;
if (operatorPlaced) {
text = text.toString().substring(0, text.length - 1);
console.log("text = " + text);
}

if (text == "") {
if (firstMinusSign && text == "") {
text += Operand = "-";
document.getElementById("outputTopLine").style.textAlign = "right";
document.getElementById("outputTopLine").innerHTML = text;
calculatorReset = false;
console.log(text + "sign");
firstMinusSign = false;
return;
}

Expand Down Expand Up @@ -93,16 +99,15 @@ evaluate = () => {
};

equals = () => {
if (!equaled) {
if (!equaled && secondOperandTurn) {
Operand = "";
document.getElementById("outputTopLine").innerHTML = text = calculator.firstOperand = evaluate();
document.getElementById("outputBottomLine").innerHTML = null;
secondOperandTurn = false;
decimalPlaced = false;
operatorTurn = true;
operatorPlaced = false;
calculator.secondOperand = null;
equaled = false;
equaled = true;
console.log("=");
}
};
Expand All @@ -117,27 +122,35 @@ AC = () => {
calculatorReset = true;
operatorTurn = false;
operatorPlaced = false;
firstMinusSign = true;
calculator.firstOperand = calculator.secondOperand = null;
console.clear();
console.log("AC");
equaled = true;
};

addDecimal = (decimal) => {
if (!decimalPlaced) {
document.getElementById("outputTopLine").style.textAlign = "right";
if (equaled && calculator.secondOperand == null) {
text = "";
console.log("dfsfd");
calculatorReset = equaled = false;
}
text += decimal.value;
Operand += decimal.value;
document.getElementById("outputTopLine").innerHTML = text;
decimalPlaced = true;
console.log("text = " + text);
}
};

clearOperand = () => {
if (calculator.secondOperand == null && operatorPlaced == false) {
if (equaled) AC();
if (text.length <= 1) AC();
else {
text = Number(text)
.toString()
.substring(0, text.length - 1);
text = Number(text).toString().substring(0, text.length - 1);
Operand = Operand.substring(0, Operand.length - 1);
calculator.firstOperand = parseFloat(Operand);
document.getElementById("outputTopLine").innerHTML = text;
Expand All @@ -157,6 +170,7 @@ clearOperand = () => {
calculator.secondOperand = null;
text = text.toString().substring(0, text.length - 1);
document.getElementById("outputTopLine").innerHTML = text;
document.getElementById("outputBottomLine").innerHTML = calculator.firstOperand;
console.log("Second Operand " + Operand);
}
};

0 comments on commit a5daf6f

Please sign in to comment.