Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Check if num==num2

  1. #1
    Join Date
    Mar 2007
    Location
    Tarboro, NC
    Posts
    290
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Check if num==num2

    Code:
    var ranNum= Math.floor(Math.random()*5);
    function getNum()
    {
    var guessNum= getElementsByName("number");
    }
    function checkNum()
    {
    if (ranNum==guessNum)
    {
    alert("You win!")
    }
    else
    {
    alert("You lose!")
    }
    }
    Will this check if inside a form the box named "number" equals the random number? I can't seem to get it working.

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Code:
    ranNum==guessNum.value
    Trinithis

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Code:
    var ranNum= Math.floor(Math.random()*5);
    var guessNum;
    function getNum(){
    	guessNum= getElementsByName("number");
    }
    
    function checkNum(){
    	if(ranNum == guessNum[0].value){
    		alert("You win!")
    	}else{
    		alert("You lose!")
    	}
    }
    I've made in guessNum variable global as in your code its scope limited to getNum function only.

    The getElementsByName() method returns a collection of objects with the specified NAME (Array of objects). Even if it returns only one object it return it as array so you need to access the object using an index.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    With some proper formatting your errors become clearer:
    Code:
    var ranNum= Math.floor(Math.random()*5);
    
    function getNum() {
      // getElementsByName() is a method of document,
      // and needs to be accessed as such.
      // Also, it returns a collection of elements.
    
      // Using an ID and getElementById() would probably
      // be more appropriate.
      var guessNum= getElementsByName("number");
    }
    
    function checkNum() {
      // guessNum was defined locally to getNum(),
      // and so isn't available here.
      if (ranNum==guessNum) {
        alert("You win!")
      } else {
        alert("You lose!")
      }
    }
    How I'd do it:
    Code:
    var NumberGuess = {
      randNum: (function() {
        var a = function() {
          var n = Math.floor(Math.random() * 5);
          a.toString = function() { return n; };
          return a;
        };
        return a();
      })(),
      checkNum: function() {
        alert(document.getElementById("number").value
            == NumberGuess.randNum
          ? "You win"
          : "You lose");
      }
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2007
    Location
    Tarboro, NC
    Posts
    290
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default

    How will it check?? I can't get it to submit the form to it, just displays the JavaScript (as expected), and if you just submit it, nothing happens. I read to code, and don't see any functions that need to be activated.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <form action="" onsubmit="NumberGuess.checkNum(); return false;">
      <div>
        <label>Guess:
          <input type="text" id="number">
        </label>
        <input type="submit" value="Check">
      </div>
    </form>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2007
    Location
    Tarboro, NC
    Posts
    290
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default

    Still doesn't work.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
      <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
      <title></title>
      <script type="JavaScript">
    var NumberGuess = {
      randNum: (function() {
        var a = function() {
          var n = Math.floor(Math.random() * 5);
          a.toString = function() { return n; };
          return a;
        };
        return a();
      })(),
      checkNum: function() {
        alert(document.getElementById("number").value
            == NumberGuess.randNum
          ? "You win"
          : "You lose");
      }
    }
      </script>
    </head>
    <body>
    <form action="" onsubmit="NumberGuess.checkNum(); return false;">
      <div>
        <label>Guess:
          <input type="text" id="number">
        </label>
        <input type="submit" value="Check">
      </div>
    </form>
    </body>
    </html>

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    Usually something like this won't hurt:

    Code:
      <script type="JavaScript">
    But, in this case it does. It should be:

    Code:
    <script type="text/javascript">
    Edit: The type attribute is required, but many browsers will let you get away without it and/or accept the decremented language attribute. However, if you do use either, it should be used correctly. Best practice is to use the type attribute only, and to write it exactly as I have shown.
    Last edited by jscheuer1; 11-22-2007 at 04:10 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The browser doesn't know what to do with a script of type "JavaScript" so ignores it.
    the decremented language attribute
    Heh, I think you mean "deprecated"
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    Heh, I think you mean "deprecated"
    Decapitated?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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