Click to See Complete Forum and Search --> : Dice Rolling Trouble


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>

JHL
06-30-2003, 08:49 PM
made some changes to your code:

script:

<script type='text/javascript'>
<!-- Begin
var die = 6;
var dice = 1;
var l = 1; //change variable name as conflict with radio button name
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;
}
return roll;
}

function treasure(obj)
{
for (var i=0; i<obj.length; i++) {
if (obj[i].checked) {
l = obj[i].value;
break;
}
}

if (l==1)
{
theroll = dice_roll(6, 1)
}
if (l==2)
{
theroll = dice_roll(8, 2)
}
document.form.answer.value = theroll;
}
// End -->
</script>

form:

<form name=form>
<input name=level type=radio value=1>1<br>
<input type=radio name=level value=2>2<br>
<input type=radio name=level value=3>3<br>
<input type=radio name=level value=4>4<br>
<input type=radio name=level value=5>5<br>
<input type=button value="Roll Dice" name=button onclick="treasure(document.form.level);">
<input type=text size=30 name=answer>
</form>


you should also change the name for your form as 'form' might be misunderstood by the browser.

jabbamonkey
06-30-2003, 11:57 PM
awesome. I was able to finish my entire script with your help (and I had over 20 levels to do ... you just saw PART of the first two levels).

Thanks.

Jabbamonkey