New Calculator
by fredericklim
HTML
<p id="sum"></p>
<p id="mul"></p>
JavaScript
/* function Calculator() {
this.a = 0;
this.b = 0;
}
function read() {
this.a = prompt("Input first value");
this.b = prompt("Input second value");
}
function sum() {
return parseInt(this.a) + parseInt(this.b);
}
function mul() {
return this.a * this.b;
}
let calculator = new Calculator();
calculator.read = read;
calculator.sum = sum;
calculator.mul = mul; */
function Calculator() {
this.read = function() {
this.a = +prompt("Input first value", 0);
this.b = +prompt("Input second value", 0);
};
this.sum = function() {
return this.a + this.b;
};
this.mul = function() {
return this.a * this.b;
};
}
let calculator = new Calculator();
calculator.read();
document.getElementById("sum").innerHTML = "Sum=" + calculator.sum();
document.getElementById("mul").innerHTML = "Mul=" + calculator.mul();