View Full Version : Yahtzee game in Javascript

12-03-2012, 05:49 AM
I'am trying to get the dice to show as number such as 1,2,3,4,5,6 in the top 5 boxes instead of the dice and save them into a array so that i can evalated the array of dice later. what am i doing wrong.
This is what i'am trying to do





function roll()
var i;
var j;

for (j=1; j<6;j++) {
i = Math.floor(Math.random()*6) + 1; //1-6
document.getElementById("d"+j).innerHTML = "<img src ='dice"+ i +".gif'/>";
<img src="dice1.gif" name="1">
<img src="dice2.gif" name="2">
<img src="dice3.gif" name="3">
<img src="dice4.gif" name="4">
<img src="dice5.gif" name="5">
<img src="dice6.gif" name="6">
var faces = new Array(



<FONT SIZE=7> Yahtzee</FONT>

<table border="1" align="center">
<td id="d1"> <img src="dice0.gif"/> </td>
<td id="d2"> <img src="dice0.gif"/> </td>
<td id="d3"> <img src="dice0.gif"/> </td>
<td id="d4"> <img src="dice0.gif"/> </td>
<td id="d5"> <img src="dice0.gif"/> </td>

<table border="1" align="center">
<tr> <td> 1's </td> <td> <input id="box1" type="text"/> </td>
</tr><tr> <td> 2's </td> <td> <input id="box2" type="text"/> </td><td></td>
<tr><td> 3's </td> <td> <input id="box3" type="text"/> </td>
<tr><td> 4's </td> <td> <input id="box4" type="text"/> </td>
<td><input type="button" value="Roll" onClick="roll()"/></td>
<tr><td> 5's </td> <td> <input id="box5" type="text"/> </td>
<tr><td> 6's </td> <td> <input id="box6" type="text"/> </td>
<tr><td> 3 of a kind </td> <td> <input id="box7" type="text"/> </td>
<tr><td> 4 of a kind </td> <td> <input id="box8" type="text"/> </td>
<tr><td> Full house </td> <td> <input id="box9" type="text"/> </td>
<tr><td> Sm straight </td> <td> <input id="box10" type="text"/> </td>
<tr><td> Lg straight </td> <td> <input id="box11" type="text"/> </td>
<td><input type="button" value="New game"/> </td></tr>
<tr><td> Yahtzee </td> <td> <input id="box12" type="text"/> </td>
<tr><td> Chance </td> <td> <input id="box13" type="text"/> </td>
<tr><td> Total </td> <td> <input id="box14" type="text"/> </td>


12-03-2012, 01:10 PM
You cannot mix Javascript and HTML code inside of a <script> tag. So first of all you'll have to remove the HTML <img> tags from the <script> tag.

But there are several more issues which make me believe you should start taking some courses first.

- Two opening <body> tags, no closing </body> tag
- Two opening <html> tags, no closing </html> tag
- You define an array "faces" that you never (need to) use
- <center> and <font> have been deprecated long time ago

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum