Next we create a simple setup:<html> <head><title>Homework Six</title></head> <body> </body> </html>
This is associated with:<script> function Listener() { this.process = function process() { alert("You don't say!..."); } } </script>
But the inside of our listener needs to focus on the problem at hand:<form> Press <input type="button" value="Proceed" onClick="a.process()"> to move on. </form> <script> a = new Listener(); a.process(); </script>
Let's start worrying about reporting the state:if (this.message) { } else { this.message = "Welcome to the game, are you ready?"; this.correct = 0; this.total = 0; this.key = ""; }
The output for this is:document.getElementById("message").innerHTML = this.message;
Now we need to get past the first screen:<span id="message"></span> <p>
This was part of the initialization. We will shuffle it later.this.questions = ["Australia", "China", "Italy", "United States", "South Africa", "Russia", "Spain", "Brazil"];
We can do this in all the screens following the first one.alert(this.questions.pop());
This is necessary later:var url = "http://silo.cs.indiana.edu:11350/images/";
Although this would be a bit better:this.question = "Whose flag is this: <img src=\"" + url + this.questions.pop() + ".png\"> " + "<input type=\"text\" name=\"answer\"> <p>" ; document.getElementById("question").innerHTML = this.question;
The addition above needs the addition listed below:this.key = this.questions.pop(); this.question = "Whose flag is this: <img src=\"" + url + this.key + ".png\"> " + "<input type=\"text\" name=\"answer\"> <p>" ; document.getElementById("question").innerHTML = this.question;
It's fair to remind you that question will have a textfield inside, so it should be in the form.<span id="question"></span>
Now this works well, but doesn't know when it's finished:
The game should also grade and report the score:if (this.questions.length > 0) { // ... } else { document.getElementById("question").innerHTML = "End of game, are you ready for a new one? <p>"; this.questions = ["Australia", "China", "Italy", "United States", "South Africa", "Russia", "Spain", "Brazil"]; }
Reporting will be done this way:if (this.key) { this.answer = document.forms[0].answer.value; if (this.answer == this.key) { this.correct += 1; this.message = "Good! "; } else { this.message = "Nope. "; } this.total += 1; } else { }
The end of game also needs to reset the state:this.message += "Score currently " + this.correct + " out of " + this.total + ".";
Finally the message needs to be updated from one game to the other:this.correct = 0; this.total = 0; this.key = "";
To randomize you can use:this.message = "Welcome to a new game, score currently: " + this.correct + " out of " + this.total + ".";
Just be sure to do it twice (when you initialize, and re-initialize).for (var i = 0; i < 100; i++) { var index = Math.floor(Math.random() * this.questions.length); var other = Math.floor(Math.random() * this.questions.length); var temp = this.questions[index]; this.questions[index] = this.questions[other]; this.questions[other] = temp; }