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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Hi im new and noob

    Im programming a dice and coin randomizer, but I cant make the coin works

    this is html with js all in one, when I press the TIRA MONEDA button it says undefined...I know its a varible but im clueless plz help

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    
    /*
    gaga
    */
    //random coin
    //preload the six images first
    var face0=new Image()
    face0.src="d1.gif"
    var face1=new Image()
    face1.src="d2.gif"
    var face2=new Image()
    face2.src="d3.gif"
    var face3=new Image()
    face3.src="d4.gif"
    var face4=new Image()
    face4.src="d5.gif"
    var face5=new Image()
    face5.src="d6.gif"
    </script>
    <img src="d1.gif" name="mydice">
    <form>
    <input type="button" value="Throw dice!" onClick="throwdice()">
    <p>
    <input type="button" value="TIRA MONEDA!" onClick="sayHello()">
    <div id="result"></div>
    <script>
    
    function sayHello(){
        document.getElementById("result").innerHTML = throwcoin();
       
    }
    
    /*function sayHello(name){
        document.getElementById("result").innerHTML = 'Hello ' + name + '!';
       
    }*/
    
    function throwdice(){
    //create a random integer between 0 and 5
    var randomdice=Math.round(Math.random()*5)
    document.images["mydice"].src=eval("face"+randomdice+".src")
    }
    
    function throwcoin(){
    //create a random integer between 0 and 1
    //this.randomcoin=Math.round(Math.random()*1)
    this.randomcoin=1;
    if(randomcoin===0){return document.getElementById("result").innerHTML = throwcoin.randomcoin;
    }
    else{return document.write('AGUILA!')}
    }
    </script>
    <title>MEMO MATH TEST</title>
    </head>
    <body>
    
    <div id="border2">
    </div>
    </body>
    </html>
    Last edited by VIPStephan; 10-11-2012 at 12:58 AM. Reason: added code BB tags

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,666
    Thanks
    6
    Thanked 1,006 Times in 979 Posts
    If you post any code please put it in between [CODE][/CODE] tags. It makes scanning your posts much easier. You can do this by clicking the small ‘#’ icon above the reply field. Also, in the future, please use a more descriptive subject when posting a question. See posting guidelines.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    This is a killer:
    Code:
    else{return document.write('AGUILA!')}
    You CAN NOT use document.write once a page is loaded. Doing so WIPES OUT the page, including even the JS code that did the document.write.

    As your code stands, once you hit that button, your page is GONE!

    There are lots of other things wrong in that code.

    You code to get a random number is all wrong. *NEVER* use Math.round() when getting a random integer.

    The correct code is:
    Code:
    var random = Math.floor( Math.random() * numberOfChoices );
    So for dice you would use
    Code:
    var randomdice = Math.floor( Math.random() * 6 );
    That will get you *GOOD* random numbers from 0 to 5. Just add 1 to get numbers from 1 to 6.

    Your HTML is totally illegal. ALL this code
    Code:
    <img src="d1.gif" name="mydice">
    <form>
    <input type="button" value="Throw dice!" onClick="throwdice()">
    <p>
    <input type="button" value="TIRA MONEDA!" onClick="sayHello()">
    <div id="result"></div>
    needs to be BETWEEN <body> and </body>. Oh, and you are missing the </form> tag.

    Use of document.images[ ] is obsolescent. Just give your images an id and then use document.getElementById().

    *NEVER* use eval(). Expecially when there is NO NEED WHATSOEVER, as in this case.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,458
    Thanks
    76
    Thanked 4,373 Times in 4,338 Posts
    Here, try this:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    //random coin
    //preload the six images first
    var dice = [];
    for ( var i = 1; i <= 6; ++i )
    {
        var d = new Image();
        d.src = "d" + i + ".gif";
        dice[i] = d;
    }
    
    function sayHello(){
        document.getElementById("result").innerHTML = throwcoin();
       
    }
    
    function throwdice(){
        //create a random integer between 1 and 6
        var randomdice = 1 + Math.floor(Math.random()*6);
        document.getElementById("mydice").src= dice[randomdice].src;
    }
    
    function throwcoin(){
        //create a random integer between 0 and 1
        return  Math.floor(Math.random()*2) == 0 ? "heads" : "tails";
    }
    </script>
    <title>MEMO MATH TEST</title>
    </head>
    <body>
    <img src="d1.gif" id="mydice">
    <form>
    <input type="button" value="Throw dice!" onClick="throwdice()">
    <p>
    <input type="button" value="TIRA MONEDA!" onClick="sayHello()">
    <div id="result"></div>
    </form>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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