Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: help with random number guessing...

1. ## help with random number guessing...

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);
var randomNumber2 = Math.random(10);
}

function verifyGuess() {
if (guessNum = sumNum)
else
// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>

<body>

<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>

• PHP Code:
``` <!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>  ```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•