Delete extra script file
This commit is contained in:
parent
aa347f1ecb
commit
85ab656c75
3 changed files with 59 additions and 210 deletions
|
|
@ -47,6 +47,6 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="script2.js"></script>
|
<script src="script.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
101
script.js
101
script.js
|
|
@ -1,34 +1,76 @@
|
||||||
const mainScreen = document.querySelector(".main-screen");
|
const mainScreen = document.querySelector(".main-screen");
|
||||||
const topScreen = document.querySelector(".top-screen");
|
const topScreen = document.querySelector(".top-screen");
|
||||||
const buttons = document.querySelectorAll("button");
|
const numButtons = document.querySelectorAll(".num");
|
||||||
|
const equalButton = document.querySelector(".equal");
|
||||||
|
const opButtons = document.querySelectorAll(".operator");
|
||||||
|
const clearButtons = document.querySelectorAll(".clear");
|
||||||
const operators = ["+", "-", "*", "/"];
|
const operators = ["+", "-", "*", "/"];
|
||||||
let inOps = false;
|
let inOps = false;
|
||||||
let isEval = false;
|
let isEval = false;
|
||||||
|
|
||||||
|
|
||||||
buttons.forEach((button) => {
|
numButtons.forEach((button) => {
|
||||||
button.addEventListener ("click", displayToScreen);
|
button.addEventListener("click", displayNum);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
opButtons.forEach((button) => {
|
||||||
|
button.addEventListener("click", displayOperator);
|
||||||
|
});
|
||||||
|
|
||||||
function displayToScreen() {
|
clearButtons.forEach((button) => {
|
||||||
console.log("inOps:", inOps);
|
button.addEventListener("click", clear);
|
||||||
console.log("isEval:", isEval);
|
});
|
||||||
|
|
||||||
if (this.className == "equal") {
|
equalButton.addEventListener("click", displayEval);
|
||||||
|
|
||||||
|
|
||||||
|
function displayEval() {
|
||||||
let calc = new Calculator;
|
let calc = new Calculator;
|
||||||
inOps = true;
|
let result = 0
|
||||||
|
|
||||||
if (isEval) {
|
if (topScreen.textContent === "0") {
|
||||||
topScreen.textContent = mainScreen.textContent;
|
calc.calculate(mainScreen.textContent);
|
||||||
isEval = false;
|
|
||||||
} else {
|
} else {
|
||||||
topScreen.textContent += mainScreen.textContent;
|
topScreen.textContent += mainScreen.textContent;
|
||||||
|
console.log("main: " + mainScreen.textContent);
|
||||||
|
result = calc.calculate(topScreen.textContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
mainScreen.textContent = calc.calculate(topScreen.textContent);
|
mainScreen.textContent = result;
|
||||||
|
inOps = false;
|
||||||
|
}
|
||||||
|
|
||||||
} else if (this.classList.contains("all")) {
|
|
||||||
|
function displayOperator() {
|
||||||
|
let result = 0
|
||||||
|
|
||||||
|
if (topScreen.textContent == "" || !inOps) {
|
||||||
|
topScreen.textContent = mainScreen.textContent + " " + this.textContent + " ";
|
||||||
|
} else {
|
||||||
|
let calc = new Calculator;
|
||||||
|
|
||||||
|
topScreen.textContent += mainScreen.textContent;
|
||||||
|
result = calc.calculate(topScreen.textContent);
|
||||||
|
|
||||||
|
topScreen.textContent = result + " " + this.textContent + " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
mainScreen.textContent = 0;
|
||||||
|
inOps = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function displayNum() {
|
||||||
|
if (mainScreen.textContent == "0") {
|
||||||
|
mainScreen.textContent = this.textContent;
|
||||||
|
} else {
|
||||||
|
mainScreen.textContent += this.textContent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function clear() {
|
||||||
|
if (this.classList.contains("all")) {
|
||||||
mainScreen.textContent = "0";
|
mainScreen.textContent = "0";
|
||||||
topScreen.textContent = "";
|
topScreen.textContent = "";
|
||||||
|
|
||||||
|
|
@ -43,39 +85,6 @@ function displayToScreen() {
|
||||||
if (mainScreen.textContent === "") {
|
if (mainScreen.textContent === "") {
|
||||||
mainScreen.textContent = "0"
|
mainScreen.textContent = "0"
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if (this.className == "operator") {
|
|
||||||
let screenContent = topScreen.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
|
|
||||||
|
|
||||||
for (let i = 0; i < screenContent.length; i++ ) {
|
|
||||||
if (operators.includes(screenContent[i])) {
|
|
||||||
let calc = new Calculator;
|
|
||||||
let result = calc.calculate(topScreen.textContent);
|
|
||||||
mainScreen.textContent = result;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
} else if ((mainScreen.textContent === "0" || inOps) && this.className == "num") {
|
|
||||||
mainScreen.textContent = this.textContent;
|
|
||||||
inOps = false;
|
|
||||||
|
|
||||||
} else if (this.className == "num" && !inOps) {
|
|
||||||
mainScreen.textContent += this.textContent;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
160
script2.js
160
script2.js
|
|
@ -1,160 +0,0 @@
|
||||||
const mainScreen = document.querySelector(".main-screen");
|
|
||||||
const topScreen = document.querySelector(".top-screen");
|
|
||||||
const numButtons = document.querySelectorAll(".num");
|
|
||||||
const equalButton = document.querySelector(".equal");
|
|
||||||
const opButtons = document.querySelectorAll(".operator");
|
|
||||||
const clearButtons = document.querySelectorAll(".clear");
|
|
||||||
const operators = ["+", "-", "*", "/"];
|
|
||||||
let inOps = false;
|
|
||||||
let isEval = false;
|
|
||||||
|
|
||||||
|
|
||||||
numButtons.forEach((button) => {
|
|
||||||
button.addEventListener("click", displayNum);
|
|
||||||
});
|
|
||||||
|
|
||||||
opButtons.forEach((button) => {
|
|
||||||
button.addEventListener("click", displayOperator);
|
|
||||||
});
|
|
||||||
|
|
||||||
clearButtons.forEach((button) => {
|
|
||||||
button.addEventListener("click", clear);
|
|
||||||
});
|
|
||||||
|
|
||||||
equalButton.addEventListener("click", displayEval);
|
|
||||||
|
|
||||||
|
|
||||||
function displayEval() {
|
|
||||||
let calc = new Calculator;
|
|
||||||
let result = 0
|
|
||||||
|
|
||||||
if (topScreen.textContent === "0") {
|
|
||||||
calc.calculate(mainScreen.textContent);
|
|
||||||
} else {
|
|
||||||
topScreen.textContent += mainScreen.textContent;
|
|
||||||
console.log("main: " + mainScreen.textContent);
|
|
||||||
result = calc.calculate(topScreen.textContent);
|
|
||||||
}
|
|
||||||
|
|
||||||
mainScreen.textContent = result;
|
|
||||||
inOps = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function displayOperator() {
|
|
||||||
let result = 0
|
|
||||||
|
|
||||||
if (topScreen.textContent == "" || !inOps) {
|
|
||||||
topScreen.textContent = mainScreen.textContent + " " + this.textContent + " ";
|
|
||||||
} else {
|
|
||||||
let calc = new Calculator;
|
|
||||||
|
|
||||||
topScreen.textContent += mainScreen.textContent;
|
|
||||||
result = calc.calculate(topScreen.textContent);
|
|
||||||
|
|
||||||
topScreen.textContent = result + " " + this.textContent + " ";
|
|
||||||
}
|
|
||||||
|
|
||||||
mainScreen.textContent = 0;
|
|
||||||
inOps = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function displayNum() {
|
|
||||||
if (mainScreen.textContent == "0") {
|
|
||||||
mainScreen.textContent = this.textContent;
|
|
||||||
} else {
|
|
||||||
mainScreen.textContent += this.textContent;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function clear() {
|
|
||||||
if (this.classList.contains("all")) {
|
|
||||||
mainScreen.textContent = "0";
|
|
||||||
topScreen.textContent = "";
|
|
||||||
|
|
||||||
} else if (this.classList.contains("error") && mainScreen.textContent !== "0") {
|
|
||||||
mainScreen.textContent = mainScreen.textContent.trimEnd()
|
|
||||||
mainScreen.textContent = mainScreen.textContent.slice(0, -1);
|
|
||||||
|
|
||||||
if (!isNaN(mainScreen.textContent.charAt(-2))) {
|
|
||||||
mainScreen.textContent = mainScreen.textContent.trimEnd()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mainScreen.textContent === "") {
|
|
||||||
mainScreen.textContent = "0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function addDecimals(a, b) {
|
|
||||||
a = String(a);
|
|
||||||
b = String(b);
|
|
||||||
|
|
||||||
const adec = (a.split(".")[1] || "").length;
|
|
||||||
const bdec = (b.split(".")[1] || "").length;
|
|
||||||
const maxDec = Math.max(adec, bdec);
|
|
||||||
let divdec = "1";
|
|
||||||
divdec = divdec.padEnd(maxDec, "0");
|
|
||||||
|
|
||||||
return ((+a * divdec + +b * divdec) / divdec).toFixed(maxDec);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function multiplyDecimals(a, b) {
|
|
||||||
a = String(a);
|
|
||||||
b = String(b);
|
|
||||||
|
|
||||||
const adec = (a.split(".")[1] || "").length;
|
|
||||||
const bdec = (b.split(".")[1] || "").length;
|
|
||||||
const totalDec = adec + bdec;
|
|
||||||
|
|
||||||
return (a * b).toFixed(totalDec)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function divideDecimals(a, b) {
|
|
||||||
a = String(a);
|
|
||||||
b = String(b);
|
|
||||||
|
|
||||||
const adec = (a.split(".")[1] || "").length;
|
|
||||||
const bdec = (b.split(".")[1] || "").length;
|
|
||||||
const maxDec = Math.max(adec, bdec);
|
|
||||||
const divisor = Math.pow(10, maxDec);
|
|
||||||
|
|
||||||
return(a * divisor) / (b * divisor);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function Calculator(str) {
|
|
||||||
console.log("Calculator has been called")
|
|
||||||
isEval = true;
|
|
||||||
|
|
||||||
this.methods = {
|
|
||||||
"-": (a, b) => addDecimals(a, -b),
|
|
||||||
"+": (a, b) => addDecimals(a, b),
|
|
||||||
"/": (a, b) => divideDecimals(a, b),
|
|
||||||
"*": (a, b) => multiplyDecimals(a, b)
|
|
||||||
};
|
|
||||||
|
|
||||||
this.calculate = function(str) {
|
|
||||||
let parts = str.split(" ");
|
|
||||||
a = +parts[0];
|
|
||||||
op = parts[1];
|
|
||||||
b = +parts[2];
|
|
||||||
|
|
||||||
|
|
||||||
if ((a || a == "0") && !b) {
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
if (isNaN(a) || isNaN(b)) {
|
|
||||||
return NaN;
|
|
||||||
}
|
|
||||||
if (!this.methods[op]) {
|
|
||||||
return "Operation not supported."
|
|
||||||
}
|
|
||||||
return this.methods[op](a, b);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue