PDA

View Full Version : help with random number guessing...

disneyfan
03-07-2005, 07:16 PM
what do I need to do to get this simple guess the sum of 2 random numbers generated exercise?

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function randNum() {
var randomNumber1 = random(10);
document.addForm.txtNum1.value = randomNumber1;
var randomNumber2 = Math.random(10);
document.addForm.txtNum2.value = randomNumber2;
}

function verifyGuess() {
var sumNum = document.addForm.txtNum1.value + document.addForm.txtNum2.value;
var guessNum = document.addForm.txtGuess.value;
if (guessNum = sumNum)
alert("Correct!");
else
alert ("incorrect");
// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>

</head>

<body>

<form name= "addForm">
<p>
<input type="text" name="txtNum1" size="20">
<input type="text" name="txtNum2" size="20">
<input type="text" name="txtGuess" size="20">
<input type="submit" value="New Problem" name="button1" onclick=randNum()>
<input type="reset" value="Verify" name="button2" onclick=verifyGuess()>
</p>
</form>

vwphillips
03-07-2005, 09:27 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>
<title></title>

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
// use indenting to aid readability
// and do not use abreviated code until you are ready

// declare variables
var randomNumber1;
var randomNumber2;
var sumNum;
var guessNum;

function randNum() {
document.getElementById('InPut').style.visibility='visible';
// The pseudorandom number generated is from 0 (inclusive) to 1 (exclusive),
// that is, the returned number can be zero, but it will always be less than one.
// The random number generator is seeded automatically when JScript is first loaded.
randomNumber1 = Math.random()*10;
document.addForm.txtNum1.value = randomNumber1;
randomNumber2 = Math.random()*10;
document.addForm.txtNum2.value = randomNumber2;
}

function verifyGuess() {
// check that the guess is a number
if (isNaN(document.addForm.txtGuess.value)){
alert('Only Numbers Allowed');
document.addForm.txtGuess.value='';
return;
}
document.getElementById('InPut').style.visibility='hidden';
// the textbox field is a string not digits so convert to digits I am using parseInt so only whole numbers used
sumNum = parseInt(document.addForm.txtNum1.value) + parseInt(document.addForm.txtNum2.value);
guessNum = document.addForm.txtGuess.value;
// == exact comparison, = make equal to
if (guessNum == sumNum){
alert("Correct!");
}
else {
alert ("incorrect\nthe answer was "+sumNum);
}
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>

</head>

<body>

<form name= "addForm">
<p>
<!-- hide the random text boxes -->
<input type="hidden" name="txtNum1" size="20">
<input type="hidden" name="txtNum2" size="20">
<br>
<!-- use standard buttons and think how the user will use it -->
<span id="InPut" style="visibility:hidden;" >Input a Number <input type="text" name="txtGuess" size="20"></span>
<input type="button" value="New Problem" name="button1" onclick=randNum()>
<input type="button" value="Verify" name="button2" onclick=verifyGuess()>
</p>
</form>
</body>
</html>