We have 15 passes and the last one thing to complete the pass says
- If 2 or more operators are entered consecutively, the operation performed should be the last operator entered
The sequence “5 * - + 5” = should produce an output of “10” : expected ‘-15’ to equal ‘10’
and we can’t seen to figure out…
for now this is how our code look like kindly help us pass the last one please.
// create out variables
var num1 = 0, num2 = 0, opr = " ",
divNumbers = document.querySelectorAll(".container .box-num"),
divOpers = document.querySelectorAll(".container .box-opr"),
showCalcBox = document.getElementById("display"),
resetBox = document.getElementById("reset"),
clearBox = document.getElementById("clear"),
// isOprClick = check if an operator is clicked
// isEqClick = check if an equals is clicked
// fc0 = first click click on operator
isOprClick = false, isEqClick = false, fco = 0;
// add action to all div
//clear show-calc letter by letter
resetBox.onclick = function () {
showCalcBox.innerHTML = showCalcBox.innerHTML.substring(0, showCalcBox.innerHTML.length -1);
};
// reset div to clear and start again
clearBox.onclick = function () {
isOprClick = false;
isEqClick = false;
fco = 0;
num1 = 0;
num2 = 0;
opr = " ";
display.innerHTML = "0";
};
// add action to show numbers
for (var i = 0; i < divNumbers.length; i++) {
divNumbers[i].onclick = function () {
if (isOprClick) {
num1 = eval(display.innerHTML);
display.innerHTML = " ";
}
// check if the text don't contain "."
if(display.innerHTML.toString().indexOf(".") === -1) {
// id text is equal tp 0 and the div pressed is not "."
if (display.innerHTML === "0" && this.innerHTML !== ".") {
display.innerHTML = this.innerHTML;
isOprClick = false;
} else {
display.innerHTML = display.innerHTML + this.innerHTML;
isOprClick = false;
}
} else if (this.innerHTML !== ".") {
display.innerHTML = display.innerHTML + this.innerHTML;
isOprClick = false;
}
}
}
// add action to do operations
for (var i = 0; i < divOpers.length; i++) {
divOpers[i].onclick = function () {
if (fco == 0) {
fco++;
num1 = eval(display.innerHTML);
// get the operator
opr = this.innerHTML;
isOprClick = true;
} else {
if (this.innerHTML!== "=") { // if the clicked div is not "="
if (!isEqClick) { // if "=" is not already clicked
num2 = eval(display.innerHTML);
display.innerHTML = calc(opr, num1, num2);
opr = this.innerHTML;
num2 = eval(display.innerHTML);
isOprClick = true;
isEqClick = false;
} else {
isEqClick = false;
opr = this.innerHTML;
}
} else {
num2 = eval(display.innerHTML);
display.innerHTML = calc(opr, num1, num2);
opr = this.innerHTML;
num1 = eval(display.innerHTML);
isOprClick = true;
isEqClick = true;
}
}
}
}
// creat a clac function to the operations
function calc(op,n1,n2) {
var result = 0;
switch(op) {
case "+":
result = n1 + n2;
break;
case "-":
result = n1 - n2;
break;
case "x":
result = n1 * n2;
break;
case "/":
if(n2 > 0)
result = n1 / n2;
break;
}
return result;
}