Add inOps variable to keep track of operations
This commit is contained in:
parent
5cb8f57879
commit
64ac97faaa
1 changed files with 13 additions and 5 deletions
18
script.js
18
script.js
|
|
@ -2,6 +2,8 @@ const mainScreen = document.querySelector(".main-screen");
|
||||||
const topScreen = document.querySelector(".top-screen");
|
const topScreen = document.querySelector(".top-screen");
|
||||||
const buttons = document.querySelectorAll("button");
|
const buttons = document.querySelectorAll("button");
|
||||||
const operators = ["+", "-", "*", "/"];
|
const operators = ["+", "-", "*", "/"];
|
||||||
|
let inOps = false;
|
||||||
|
|
||||||
|
|
||||||
buttons.forEach((button) => {
|
buttons.forEach((button) => {
|
||||||
button.addEventListener ("click", displayToScreen);
|
button.addEventListener ("click", displayToScreen);
|
||||||
|
|
@ -9,14 +11,19 @@ buttons.forEach((button) => {
|
||||||
|
|
||||||
|
|
||||||
function displayToScreen() {
|
function displayToScreen() {
|
||||||
|
console.log(inOps);
|
||||||
|
|
||||||
if (this.className == "equal") {
|
if (this.className == "equal") {
|
||||||
let calc = new Calculator;
|
let calc = new Calculator;
|
||||||
topScreen.textContent += mainScreen.textContent;
|
inOps = true;
|
||||||
|
|
||||||
|
topScreen.textContent += mainScreen.textContent;
|
||||||
mainScreen.textContent = calc.calculate(topScreen.textContent);
|
mainScreen.textContent = calc.calculate(topScreen.textContent);
|
||||||
|
|
||||||
} else if (this.classList.contains("all")) {
|
} else if (this.classList.contains("all")) {
|
||||||
mainScreen.textContent = "0";
|
mainScreen.textContent = "0";
|
||||||
topScreen.textContent = "";
|
topScreen.textContent = "";
|
||||||
|
|
||||||
} else if (this.classList.contains("error") && mainScreen.textContent !== "0") {
|
} else if (this.classList.contains("error") && mainScreen.textContent !== "0") {
|
||||||
mainScreen.textContent = mainScreen.textContent.trimEnd()
|
mainScreen.textContent = mainScreen.textContent.trimEnd()
|
||||||
mainScreen.textContent = mainScreen.textContent.slice(0, -1);
|
mainScreen.textContent = mainScreen.textContent.slice(0, -1);
|
||||||
|
|
@ -32,7 +39,7 @@ function displayToScreen() {
|
||||||
if (this.className == "operator") {
|
if (this.className == "operator") {
|
||||||
let screenContent = topScreen.textContent;
|
let screenContent = topScreen.textContent;
|
||||||
topScreen.textContent += mainScreen.textContent + " " + this.textContent + " ";
|
topScreen.textContent += mainScreen.textContent + " " + this.textContent + " ";
|
||||||
|
inOps = true;
|
||||||
// Add some logic for handling negatives with -
|
// Add some logic for handling negatives with -
|
||||||
// Add key support
|
// Add key support
|
||||||
// limit character size in mainScreen -- first step is to move numbers from the main mainScreen to somewhere else every time a new operator is used and then change what gets sent to the calculator to that new location
|
// limit character size in mainScreen -- first step is to move numbers from the main mainScreen to somewhere else every time a new operator is used and then change what gets sent to the calculator to that new location
|
||||||
|
|
@ -40,7 +47,6 @@ function displayToScreen() {
|
||||||
for (let i = 0; i < screenContent.length; i++ ) {
|
for (let i = 0; i < screenContent.length; i++ ) {
|
||||||
if (operators.includes(screenContent[i])) {
|
if (operators.includes(screenContent[i])) {
|
||||||
let calc = new Calculator;
|
let calc = new Calculator;
|
||||||
|
|
||||||
result = calc.calculate(topScreen.textContent);
|
result = calc.calculate(topScreen.textContent);
|
||||||
mainScreen.textContent = result;
|
mainScreen.textContent = result;
|
||||||
break;
|
break;
|
||||||
|
|
@ -48,9 +54,11 @@ function displayToScreen() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} else if ((mainScreen.textContent === "0") && this.className == "num") {
|
} else if ((mainScreen.textContent === "0" || inOps) && this.className == "num") {
|
||||||
mainScreen.textContent = this.textContent;
|
mainScreen.textContent = this.textContent;
|
||||||
} else if (this.className == "num") {
|
inOps = false;
|
||||||
|
|
||||||
|
} else if (this.className == "num" && !inOps) {
|
||||||
mainScreen.textContent += this.textContent;
|
mainScreen.textContent += this.textContent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue