Jump to content

rolling multiple dice


mwbarry

Recommended Posts

Im practicing js by writing a function that will first ask the user how many dice they would like to roll and then outputs the values of each dice in an alert box. If the user enters "3" into the prompt, the function will return something like "5, 2, 6".I Have everything working except I can't figure out how to generate a number 1-6 x amount of times without writing excessively long static code.This code outputs one number x amount of times each in a different alert box, but how do i get x amount of dice rolls to show up in one alert box?

<script type="text/javascript">	function showdice() {		var dicenum = prompt("how many dice would you like to roll?");		var diceint = Math.round(dicenum);		var diceroll = Math.ceil(Math.random() * 6);				if (diceint >= "7") {			alert("we don't have that many dice");		}		else if (diceint <= "0") {			alert("you need to roll at least one die");		}		else			for (i = 1; i <= diceint; i++) {				alert(diceroll);			}	}</script>

Link to comment
Share on other sites

just concatenate all the results into one string and just alert that.

		var results = '';for (i = 1; i <= diceint; i++) {  results .= diceroll + ',';};alert(results);

you might want to consider just casting everything to numbers rather than mixing between strings and ints.

Link to comment
Share on other sites

for (i = 1; i <= diceint; i++) { results .= diceroll + ',';};
did you intend to write '.='? I don't believe there is a '.=' operator.I put just '=' instead of '.=' but then only received an alert stating one dice roll. The problem is that there is only 1 random number being generated. Is there a way to generate more without having to make variables for dice1, dice2, dice3 etc... and then telling the browser how many dice to output?How would I go about telling the browser to create x amount of variables (so that we could theoretically roll thousands of dice), each being a random number 1-6 and then outputting each number in one alert box?
Link to comment
Share on other sites

he meant +=, .= is the PHP version of JS's += :)to generate different random values, the Math.random() function needs to be inside the loop:

var diceroll, results = '';for (i = 1; i <= diceint; i++) {	diceroll = Math.ceil(Math.random() * 6);	results += diceroll + ',';}alert(results);

if you need to store the random values, you can use an array instead of multiple variables:

var results = [];for (i = 0; i < diceint; i++) {	results[i] = Math.ceil(Math.random() * 6);}alert(results.join(', '));

Link to comment
Share on other sites

Awesome thanks for the help. The function is working perfectly. Even adjusted it a bit so it has perfect grammar outputting 'your numbers are 4, 6, 3 and 4.' rather than 'your numbers are 4, 6, 3, 4,'I am now trying to figure out how to total these numbers. I was thinking to create an array called 'sum' with no default values, populate the array with the dice values, and add them together. and then append "\n 'the total is ' + sum" I have very little experience with arrays and have no idea how I would go about implementing this.

function showdice() {	var dicenum = prompt("how many dice would you like to roll?");	var diceint = Math.round(dicenum);	var diceroll = '';	var lastroll = Math.ceil(Math.random() * 6);	var lastroll2 = Math.ceil(Math.random() * 6);	var results = '';	var sum = Array('');		for (x = 1; x <= diceint -2; x++) {		diceroll = Math.ceil(Math.random() *6);		results += diceroll + ', ';		sum += diceroll;	}	if (diceint < '1')	{		alert('you need to roll at least one die');	}	else if (diceint == '1') {		alert('your number is ' + lastroll);	}	else		alert('your numbers are ' + results + lastroll + ' and ' + lastroll2 + '. \n' + 'total = ' + sum);}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...