Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-10-2012, 07:38 PM   PM User | #1
memo333
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
memo333 is an unknown quantity at this point
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
memo333 is offline   Reply With Quote
Old 10-11-2012, 12:59 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,601
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 10-11-2012, 01:51 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 10-11-2012, 01:56 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:50 AM.


Advertisement
Log in to turn off these ads.