This commit is contained in:
boris
2025-02-06 12:28:24 +00:00
parent 2238305d65
commit f30a8fe711
133 changed files with 1262 additions and 8255 deletions

40
js_workshop6/index.js Normal file
View File

@@ -0,0 +1,40 @@
// Creates the Calculator Object
function Calculator() {
this.number1 = 0;
this.number2 = 0;
this.op = 'add';
this.add = function() { return this.number1 + this.number2; }
this.minus = function() { return this.number1 - this.number2; }
this.multiply = function() { return this.number1 * this.number2; }
this.divide = function() { return this.number1 / this.number2; }
this.Calculate = function getResult() {
if (this.op === 'add') return this.add();
if (this.op === "mult") return this.multiply();
if (this.op === "minus") return this.minus();
if (this.op === "div") return this.divide()
}
}
// Create Event Listener for button
document.getElementById('calculate').addEventListener('click', getResult, false);
// Instantiate Calculator
let calculator = new Calculator();
// Validates input and sets disabled input box to result
function getResult() {
// handler function for the calcButton
if (document.getElementById("num1").value === "") {
// show error or make a page element show the error
alert("Enter some values")
return;
}
// Give object variables values.
calculator.number1 = parseFloat(document.getElementById("num1").value);
calculator.number2 = parseFloat(document.getElementById("num2").value);
calculator.op = document.getElementById("operator").value;
// This is pretty cool, + typecasts to Number, I multiply by 100, then divide by 100,
// while avoiding rounding errors!
document.getElementById('result').value = +(Math.round(calculator.Calculate() + "e+2") + "e-2");
}