Create secondary script for troubleshooting and code rewrite
This commit is contained in:
parent
64ac97faaa
commit
f94479f2d2
3 changed files with 154 additions and 5 deletions
24
script.js
24
script.js
|
|
@ -3,6 +3,7 @@ const topScreen = document.querySelector(".top-screen");
|
|||
const buttons = document.querySelectorAll("button");
|
||||
const operators = ["+", "-", "*", "/"];
|
||||
let inOps = false;
|
||||
let isEval = false;
|
||||
|
||||
|
||||
buttons.forEach((button) => {
|
||||
|
|
@ -11,13 +12,20 @@ buttons.forEach((button) => {
|
|||
|
||||
|
||||
function displayToScreen() {
|
||||
console.log(inOps);
|
||||
console.log("inOps:", inOps);
|
||||
console.log("isEval:", isEval);
|
||||
|
||||
if (this.className == "equal") {
|
||||
let calc = new Calculator;
|
||||
inOps = true;
|
||||
|
||||
topScreen.textContent += mainScreen.textContent;
|
||||
if (isEval) {
|
||||
topScreen.textContent = mainScreen.textContent;
|
||||
isEval = false;
|
||||
} else {
|
||||
topScreen.textContent += mainScreen.textContent;
|
||||
}
|
||||
|
||||
mainScreen.textContent = calc.calculate(topScreen.textContent);
|
||||
|
||||
} else if (this.classList.contains("all")) {
|
||||
|
|
@ -38,8 +46,15 @@ function displayToScreen() {
|
|||
} else {
|
||||
if (this.className == "operator") {
|
||||
let screenContent = topScreen.textContent;
|
||||
topScreen.textContent += mainScreen.textContent + " " + this.textContent + " ";
|
||||
inOps = true;
|
||||
|
||||
if (isEval) {
|
||||
topScreen.textContent = mainScreen.textContent + " " + this.textContent + " "
|
||||
isEval = false
|
||||
} else {
|
||||
topScreen.textContent += mainScreen.textContent + " " + this.textContent + " ";
|
||||
}
|
||||
|
||||
// Add some logic for handling negatives with -
|
||||
// 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
|
||||
|
|
@ -47,7 +62,7 @@ function displayToScreen() {
|
|||
for (let i = 0; i < screenContent.length; i++ ) {
|
||||
if (operators.includes(screenContent[i])) {
|
||||
let calc = new Calculator;
|
||||
result = calc.calculate(topScreen.textContent);
|
||||
let result = calc.calculate(topScreen.textContent);
|
||||
mainScreen.textContent = result;
|
||||
break;
|
||||
}
|
||||
|
|
@ -106,6 +121,7 @@ function divideDecimals(a, b) {
|
|||
|
||||
function Calculator(str) {
|
||||
console.log("Calculator has been called")
|
||||
isEval = true;
|
||||
|
||||
this.methods = {
|
||||
"-": (a, b) => addDecimals(a, -b),
|
||||
|
|
|
|||
Loading…
Reference in a new issue