← Back to all posts
have an assignment, conditional dice roll with average

must create a Web page that simulates a large number of dice rolls (each dice roll involves rolling two dice) and computes the average of the dice totals. The user should be able to enter a number of rolls in a text box and then click a button, which should call a function to simulate the rolls and compute the average. Once calculated, the average should be displayed in a separate text box. please help here's the code i have thus far, but doesn't seem to be working right

``````<html>
<!-- avgr.html Dave Reed -->
<!--------------------------------------------------------->

<title> Averages </title>
<script type="text/javascript"
src="scripts/random.js"></script>

<script type ="text/javascript">
function roll(numOfRolls)
{
// Initalize function variables and a history array
var totals=r=0, rollHist=new Array(0,0,0,0,0,0);

for(i=0; i < numOfRolls; i++)
{
r=RandomInt(1, 6);
rollHist[r-1]++; // rollHist[r] value is incremented
totals+=r; // add the value or r to the totals
} // end of for loop

var avg = totals / numOfRolls; // calc the average from the total

document.form1.average.value=avg; // update the text box
// update the roll hits history display
for(c=0; c<6; c++)
document.form1.rollHistory[c].value=rollHist[c];
} // end of function roll

</script>

<body>
<form name="form1">
Desired number of rolls =
<input type="text" name="reps" size="6" value="1000" />
<br><br>
Average =
<input type="text" name="average" size="6" value="0" />
<br><br>
<input type="button" name="button" value="Roll Me" onclick="roll(document.form1.reps.value-0);">
<br><br>Total of rolled numbers:-<br>
No. Hits.<br>
1. <input type="text" name="rollHistory" size="6" value="0" /><br>
2. <input type="text" name="rollHistory" size="6" value="0" /><br>
3. <input type="text" name="rollHistory" size="6" value="0" /><br>
4. <input type="text" name="rollHistory" size="6" value="0" /><br>
5. <input type="text" name="rollHistory" size="6" value="0" /><br>
6. <input type="text" name="rollHistory" size="6" value="0" /><br>
</form>
</body>
</html>``````
Vandesm14 (2433)

Can you put your code into a repl?

likeray (0)

it will not fit, better yet. I figured how to make the page, where I'm stuck now is turning it into a successful "while loop" @Vandesm14 here's the for loop i made that works well, but how can I make an equivalent while loop to display same outcomes?

``````<script type ="text/javascript">
function roll(numOfRolls)
{
// Initalize function variables and a history array
var totals=r=0, rollHist=new Array(0,0,0,0,0,0);

for(i=0; i < numOfRolls; i++)
{
r=RandomInt(1, 6);
rollHist[r-1]++; // rollHist[r] value is incremented
totals+=r; // add the value or r to the totals
} // end of for loop

var avg = totals / numOfRolls; // calc the average from the total

document.form1.average.value=avg; // update the text box
// update the roll hits history display
for(c=0; c<6; c++)
document.form1.rollHistory[c].value=rollHist[c];
} // end of function roll

</script>``````
Vandesm14 (2433)

@likeray What do you mean it won't fit into a repl? Just copy+paste the code and share the link with me. Your code uses references to the DOM, which is only possible while paired with HTML.

Make a repl: https://repl.it/languages/html

Vandesm14 (2433)

@likeray I cannot view anything as there's no console.logs or any form elements inside your repl. Please update the repl so I can help you.

likeray (0)

https://repl.it/talk/share/for-loop-into-while-loop/23036

Vandesm14 (2433)

@likeray You did not define the function "RandomInt". If you need a function to generate a number from x to y, use this `Math.round(Math.random() * x) + y`

likeray (0)

i have everything defined in a .js file on desktop so for loop works on my end @Vandesm14 just need logistics on how to properly plug in what i have to demonstrate whileloop

Vandesm14 (2433)

@likeray Then upload the js file to repl so I can help you. Just add a file to the repl and link the file by doing this: `<script src="name-of-file.js"></script>`

likeray (0)

ok, I got the .js file part of that HTML, you can see it working as a for loop @Vandesm14

likeray (0)

any help here would be amazing, getting this far was a feat. how would I properly plug in RandomInt? Is it possible to make whileloop without needing RandomInt @Vandesm14 ?

Vandesm14 (2433)

@likeray Is RandomInt a custom function? You should include your functions and such before the main javascript file as per your html:

``````<script src="funcions.js"><script>
<script src="main.js"><script>``````
likeray (0)

going so far from helping me turn this into a while loop. if no longer interested in helping, not a problem. I clearly am a poor programmer, no need to piss on my logic further. just a person who got in far passed his prime struggling to know up from down, if there's no way to revise what I have into a while loop, just be honest @Vandesm14