Thread: javascript error?
jammaster82


<html>


<script type="text/

javascript

 " language="

javascript

 ">

function RollDice()
{
var randomnumber = Math.floor(Math.random()*6)+1; /* generates a random number from 0 to 5, if you want to get a number between 0 and 100 just change the 6 to 1001 */
                document.write(randomnumber);
                return true;
}


</script>


<input type="button" value="Roll A Die" onclick="RollDice()" >
</html>





it gives me the random number but then says 'loading..'

if i do an alert() instead of document.write it returns okay...

How would i add lines to the document inside that function
and return, not that i need to do it or anything but always love
to

learn

 .. why does it hang on the document.write?

DangerMouse

I know nowt about javascipt, but I'd imagine its something to do with scope? Variables are normally private to a function, in this instance write is being called on a 'document' object, but that 'document' object is not visible.

I could be totally barking up the wrong tree here, its just a guess.

DM

manu`


<html>
<script type="text/

javascript

 " language="

javascript

 ">
function RollDice()
{
var randomnumber = Math.floor(Math.random()*7);
if(randomnumber == 0)
{
randomnumer++;
}
document.forms[0].dice.value = randomnumber;
}
</script>


<form name="yourname" method="get">
<input type="button" value="Roll A Die" onclick="RollDice()" >
<input type="text" name="dice" value="" readonly>
</form>
</html>


this should work.

jammaster82

AH,... thank you both...




function RollDice()
{
var randomnumber = Math.floor(Math.random()*7);
if(randomnumber == 0)
{
randomnumber++;
}
document.forms[0].dice.value = randomnumber;
}


does this not produce 7 different random numbers
at first until modified,returning a 1 for both 0 and 1?

in the above quoted code arent there
eight possible random numbers at first?

--> 0,1,2,3,4,5,6 and 7 ?

manu`

change randomnumber++ to randomnumber = Math.floor(Math.random()*7);
now it generates a new random number from 1 to 6

jammaster82

what if its zero again?>


Perkiset's Place Home   Politics @ Perkiset's