Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Thanked 0 Times in 0 Posts

    Yahtzee game in Javascript

    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>

  2. #2
    Senior Coder
    Join Date
    Dec 2010
    Thanked 570 Times in 563 Posts
    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts