Share
Explore BrainMass

A simple game written in javascript using random numbers

Please see the attached file.

Design and implement a Web page named esp.html that allows the user to conduct an ESP test. Your page should look something like the one in Figure 11.10. The rectangle containing a question mark represents the back of a card that is associated with a particular shape. After contemplating which shape appears on the front of the card, the user clicks the button corresponding tot he shape he or she believes is on the card (here, four possibilities are assumed). After each guess, the question mark should be replaced by a randomly selected shape, and an alert box should notify the user as to whether the guess was correct. Statistics concerning the number and percentage of correct guesses should be maintained in text boxes. After the statistics have been updated and the alert box has been closed, the page should restore the question-mark image, enabling the user to enter another guess.

Attachments

Solution Preview

The Javascript for this solution is show in the code below. Comments in the code indicate what is happening with each group of statements.

function esp(choice)
{
// First we pick a random id that corresponds to an image
var randomId = Math.floor(Math.random()*4+1);

// Display the image. An id of 1 = square, 2 = triangle, etc.
if (randomId == 1) {
displayImage("square");
}
if (randomId == 2) {
displayImage("triangle");
}
if (randomId == 3) {
displayImage("circle");
}
if (randomId == 4) {
displayImage("star");
}

...

Solution Summary

This solution creates a simple game written in javascript. It demonstrates how to use functions and random numbers. It also shows how to link javascript to a button.

$2.19