switch (this.operator) {
case "+":
this.solution = firstOper + secondOper;
break;
case "-":
if (firstOper < secondOper) {
tmp = firstOper;
firstOper = secondOper;
secondOper = tmp;
}
this.solution = firstOper - secondOper;
break;
case "*":
this.solution = firstOper * secondOper;
break;
case "/":
if (secondOper == 0) {
secondOper++;
}
tmp = firstOper * secondOper;
this.solution = firstOper;
firstOper = tmp;
}
this.equation = firstOper + this.operator + secondOper + "="
};
Professor: There’s nothing to discuss here, I guess. What a bout the second part? How
did you connec t the generator to the HTML that I gave you?
Mike: We needed to call the getElementByID () method many times so we d ecided
to make the code a bit shorter by d efining the next w rapper function:
var getElement = function(id) {
return document.getElementById(id);
};
Using this function, we produc ed the following code:
window.onload = function() {
var ws = new MathWorksheet();
var score = 0;
ws.setOperator("*");
ws.setMaxVal(10);
ws.generate();
getElement("eq0-label").innerHTML = ws.getEquation();
getElement("submit").onclick = function() {
if (ws.getSolution() == parseInt(getElement("eq0").value)) {
score++;
}
246 Meeting 13. User Interface