Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,501
    Thanks
    3
    Thanked 500 Times in 487 Posts
    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> 
    Last edited by vwphillips; 03-07-2005 at 09:33 PM.


  •  

    Posting Permissions

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