jabbamonkey
06-30-2003, 06:36 PM
Ok, first off, I am very much a newbie with Javascript.
Now,... I'm trying to use a web form to generate some random variables. The web form lets the user select 1 of 20 radio boxes (name=level). When the user hits submit, it runs treasures(level)
The treasure function processes the level the user chooses. For level 1, I want the output to be one roll of a six sided die. If level 2, two rolls of an eight sided die. I plan on adding much more later on, but could use some help with just these two levels...
My problem is that whenever I run the script (see below), the form field always places in whatever the variable for "theroll" is. In the case below, it always outputs "0" (and NOT any roll of the dice)
Here is the script that is in the head of my page....
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var die = 6;
var dice = 1;
var level = 1;
var theroll = 0;
function dice_roll(die, dice)
{
var roll = 0;
for (loop=0; loop < dice; loop++)
{
roll = roll + Math.round(Math.random()*(die - 1))+1;
}
}
function treasure(level)
{
if (level==1)
{
theroll = dice_roll(6, 1)
}
if (level==2)
{
theroll = dice_roll(8, 2)
}
document.form.answer.value = theroll;
}
// End -->
</script>
Here is the form....
<form name=form>
<input name=level type=radio onclick="level = 1">1<br>
<input type=radio name=level onclick="level = 2">2<br>
<input type=radio name=level onclick="level = 3">3<br>
<input type=radio name=level onclick="level = 4">4<br>
<input type=radio name=level onclick="level = 5">5<br>
<input type=button value="Roll Dice" name=button onclick="treasure(level)">
<input type=text size=30 name=answer>
</form>
Now,... I'm trying to use a web form to generate some random variables. The web form lets the user select 1 of 20 radio boxes (name=level). When the user hits submit, it runs treasures(level)
The treasure function processes the level the user chooses. For level 1, I want the output to be one roll of a six sided die. If level 2, two rolls of an eight sided die. I plan on adding much more later on, but could use some help with just these two levels...
My problem is that whenever I run the script (see below), the form field always places in whatever the variable for "theroll" is. In the case below, it always outputs "0" (and NOT any roll of the dice)
Here is the script that is in the head of my page....
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var die = 6;
var dice = 1;
var level = 1;
var theroll = 0;
function dice_roll(die, dice)
{
var roll = 0;
for (loop=0; loop < dice; loop++)
{
roll = roll + Math.round(Math.random()*(die - 1))+1;
}
}
function treasure(level)
{
if (level==1)
{
theroll = dice_roll(6, 1)
}
if (level==2)
{
theroll = dice_roll(8, 2)
}
document.form.answer.value = theroll;
}
// End -->
</script>
Here is the form....
<form name=form>
<input name=level type=radio onclick="level = 1">1<br>
<input type=radio name=level onclick="level = 2">2<br>
<input type=radio name=level onclick="level = 3">3<br>
<input type=radio name=level onclick="level = 4">4<br>
<input type=radio name=level onclick="level = 5">5<br>
<input type=button value="Roll Dice" name=button onclick="treasure(level)">
<input type=text size=30 name=answer>
</form>