Add condition to check for existing operators on screen
This commit is contained in:
parent
a317e11d90
commit
dc49e65560
1 changed files with 17 additions and 4 deletions
21
script.js
21
script.js
|
|
@ -1,5 +1,6 @@
|
||||||
const screen = document.querySelector(".screen")
|
const screen = document.querySelector(".screen");
|
||||||
const buttons = document.querySelectorAll("button")
|
const buttons = document.querySelectorAll("button");
|
||||||
|
const operators = ["+", "-", "*", "/"];
|
||||||
|
|
||||||
buttons.forEach((button) => {
|
buttons.forEach((button) => {
|
||||||
button.addEventListener ("click", displayToScreen);
|
button.addEventListener ("click", displayToScreen);
|
||||||
|
|
@ -10,9 +11,21 @@ function displayToScreen() {
|
||||||
if (this.className == "equal") {
|
if (this.className == "equal") {
|
||||||
//equal
|
//equal
|
||||||
} else if (this.className == "clear") {
|
} else if (this.className == "clear") {
|
||||||
//clear
|
//clear´
|
||||||
} else {
|
} else {
|
||||||
screen.textContent = this.textContent;
|
let screenContent = screen.textContent;
|
||||||
|
|
||||||
|
if (this.className == "operator") {
|
||||||
|
for (let i = 0; i < screenContent.length; i++ ) {
|
||||||
|
if (operators.includes(screenContent[i])) {
|
||||||
|
//call calculator
|
||||||
|
console.log("An operator was pressed again")
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
screen.textContent += this.textContent;
|
||||||
console.log("A button was pressed")
|
console.log("A button was pressed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue