Ugh, what horrible code. A class, you say?
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Problem 2</title>
<script type="text/javascript">
function NumberGame() {
this.n1 = this.n2 = this.target = this.modifier1 = this.modifier2 = 0;
this.upper1 = this.upper2 = 1;
this.won = true;
this.message = "What is %1 plus %2?";
this.fail = "Sorry, try again!";
this.succeed = "Good job!";
}
NumberGame.prototype.construct = function() {
this.n1 = Math.floor(Math.random() * (this.upper1 + 1)) + modifier1;
this.n2 = Math.floor(Math.random() * (this.upper2 + 1)) + modifier2;
this.target = this.n1 + this.n2;
this.message = this.message.replace(/%1/g, this.n1).replace(/%2/g, this.n2);
this.won = false;
}
NumberGame.prototype.play = function() {
this.construct();
while(!this.won) {
this.won = (this.target == prompt(this.message));
if(!this.won) alert(this.fail);
}
window.alert(this.succeed);
}
var game = new NumberGame();
game.upper1 = 10;
game.upper2 = 6;
game.play();
</script>
</head>
<body>
</body>
</html>
Bookmarks