
Originally Posted by
jscheuer1
Your code looks OK but, there may be problems with IE confusing variable names with element objects in the implied document.all collection. To tell if this is the case or if there is some other problem, I would need to see the markup that goes with the script.
Here is the entire markup. Thanks for taking a look.
HTML Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Guessing Game</title>
<!--
The following JavaScript program generates a random number between 1 and 100.
The user then enters their guess at that number. They receive messages to help
them get to the generated number and are congratulated when they get it correct and
are asked if they would like to play the game again. If they click OK, a new number is
generated and the game starts all over again. If they click Cancel, the application closes.
If, after a new number is generated, the user does not make a guess for 10 consecutive seconds,
a message will be displayed asking if the user wants to continue play or if they want to exit
the game. If they select to continue play, a new number will be generated.
-->
<style type="text/css">
body {
text-align: center;
background-color: #d1d1d1;
}
.guessEntry {
text-align: center;
}
</style>
<script type="text/javascript">
<!--
// declare variables
var num = 0 // stores the randomly generated number
var t // variable for setTimeout
var counter = 0 // stores the number of valid guesses made
// small function to clear the user's guess and set focus back to text area
function clearGuess() {
document.frmGuess.userGuess.value = "";
document.frmGuess.userGuess.focus();
}
// function to generate the random number between 1 and 100
function generateNum() {
setTimer();
clearGuess();
counter = 0;
num = Math.round(Math.random() * 100);
}
// function to handle the users guess
function guess() {
clearTimeout(t);
var userGuess = parseInt(document.frmGuess.userGuess.value);
// tell the user if they do not enter a number between 1 and 100
if (userGuess < 1 || userGuess > 100) {
window.alert("You must enter a number between 1 and 100");
clearGuess();
return false;
}
// tell the user if they do not enter a numeric value
if (isNaN(userGuess)) {
window.alert("You must enter a numeric value");
clearGuess();
return false;
}
// increment the counter and set the text to use depending on count
++counter
if (counter == 1) {
var guessString = " guess";
}
else {
var guessString = " guesses";
}
// congratulate the user if they guess the correct number and ask if
// they want to play again
if (userGuess == num) {
result = confirm("Correct!!!!!! " + userGuess + " was the number and "
+ "it only took you " + counter + guessString + "!!\n\n"
+ "Click OK to play again or Cancel to exit the game.");
if (result) {
generateNum();
}
else {
window.close();
}
}
// tell the user if their guess is greater than the target number
else if (userGuess > num) {
alert("Less than " + userGuess);
clearGuess();
}
// tell the user if their guess is less than the target number
else {
alert("Greater than " + userGuess);
clearGuess();
}
}
// function to start a 10 second counter.
function setTimer() {
t = setTimeout("contin()",10000);
}
// if user does not make a guess in 10 seconds, they are asked if they would like to exit
// the game or continue playing
function contin() {
result = confirm("Do you wish to exit this game? Click OK to exit "
+ "or Cancel to continue play.");
if (result) {
window.close();
}
else {
generateNum();
}
}
-->
</script>
</head>
<body onload="generateNum();">
<h2>Guess the computer's number</h2>
<h4>Enter your guess (between 1 and 100) in the area below and click <tt>Guess</tt></h4>
<form name="frmGuess" action="">
<input class="guessEntry" type="text" name="userGuess" />
<br /><br /><br />
<input type="button" name="submitGuess" value="Guess" onclick="guess();" />
<input type="button" name="generateNew" value="New Number" onclick="generateNum();" />
<input type="button" name="exit" value="Exit Game" onclick="window.close();" />
</form>
</body>
</html>
Bookmarks