// 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"); }