Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Citanu541
Dec 1, 2008
So, I'm not necessarily new to JavaScript, but I was given an assignment that I just cannot figure out. Basically it's the age old dice roll game that we were all given, I'm sure. The teacher asked us to generate random dice rolls, display each roll into a separate alert box and then display each roll onto the document. This all sounds simple so far, I know, but the part that I just can't figure out is that he wants us to display each roll separated by a comma and stick "and" between the second to last and last numbers. (example: 4, 6, 3, 6, and 4) Which stills sounds simple until he tells us we can't use arrays to complete the task! I have to be honest with the fact that I don't know how to do this with out arrays! I'm too used to using them at this point in my life. Really we're limited to loops and if statements. If anybody has any insight on how I can go about doing this I'd appreciate it. I can post my code if that would help and I'm not even asking for a direct answer, I just don't know how to go about completing the task, just pointing me in the right direction would make me grateful.

edit: posted code, anyway

code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html>

<head>
<title>Dice Roll - </title>

<script type="text/javascript">

var counter = 0;
var fName = prompt("Please enter your name: ");
var diceNum = prompt("Please enter the number of dice you would like to roll: ");
var total =0;



document.write("Rolling " + diceNum + " dice. \n\n");
document.write("Hey, " + fName + " you rolled a "); 
	for(counter = 1; counter <= diceNum; counter++)
		{
		var random = (Math.floor(Math.random() * 7) + 1);
		
		alert("Dice # " + counter + ": " + random);
		total = random + total;		
		document.write("" + random + ", ");
		
		}
document.write(" and " + random + " Your total is " + total + ".");
</script>

</head>

</html>
I realize the document.write statements are very wrong, but I wanted to make it look like I had made some progress =/

Citanu541 fucked around with this message at 06:12 on Feb 8, 2009

Adbot
ADBOT LOVES YOU

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply