CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Hi im new and noob (http://www.codingforums.com/showthread.php?t=276049)

memo333 10-10-2012 07:38 PM

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>


VIPStephan 10-11-2012 12:59 AM

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.

Old Pedant 10-11-2012 01:51 AM

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.

Old Pedant 10-11-2012 01:56 AM

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>



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

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.