Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Dice Rolling Game

Options
  • 25-03-2012 11:42pm
    #1
    Closed Accounts Posts: 2,663 ✭✭✭


    Need Help can some one please tell me how i can place a Document.write() into a Table Form.. in the Same Page without Loading a new one

    
      function playGame () {
       
       
       
       document.write('<table border="1" cellspacing="1" cellpadding="5">')
       
          document.write('<tr>')
       document.write('<td>1 Was Rolled </td>  ' +  '<td>totalArray[0] +  Times. </td>')
       document.write('<td>row ' + i + ', column 1</td>')
       document.write('<td>row ' + i + ', column 2</td>')
       document.write('</tr>')
       
       
     //  document.write (' 1 was rolled ' + totalArray[0] + ' times. ');
        
         
         // document.write (' 2 was rolled ' + totalArray[1] + ' times.');
        
         
         // document.write (' 3 was rolled ' + totalArray[2] + ' times.');
        
         
          //document.write (' 4 was rolled ' + totalArray[3] + ' times.');
        
         
          //document.write (' 5 was rolled ' + totalArray[4] + ' times.');
        
         
         // document.write (' 6 was rolled ' + totalArray[5] + ' times.');
        
         
          //document.close ();
      }
    
    document.write('</table>')
    

    Another Thing can some one please point me to the Right Direction on Compound Interest Calculator on Java Script.. So far all i have is
    <ul class="form">
    <li>
    <label for="period">Number of Years to Show (years)</label><br />
    <input type="text" id="period" placeholder="years"  autocomplete="off" size="8">
    </li>
    <br />
    <li>
    <label for="interest">Interest Rate</label><br />
    <input type="text" id="interest" placeholder="Interest %" autocomplete="off" size="8">
    </li>
    <br />
    <li>
    <label for="initial">Investment(&#8364;)</label><br />
    <input type="text" id="initial" placeholder="Investment" autocomplete="off" size="10">
    <br />
    </li>
    </ul>
    
    
    


Comments

  • Registered Users Posts: 131 ✭✭CuAnnan


    You don't use document.write. You use document.innerHTML

    [PHP]
    function doSomething()
    {
    someHTML = "<br>Example</br>";
    document.getElementById("someDiv").innerHTML += someHTML;
    }
    [/PHP]

    [HTML]
    <div id="someDiv"></div>
    [/HTML]


  • Registered Users Posts: 131 ✭✭CuAnnan


    The formula for calculating anual compound interest is ((100 + rate)/100) ^ years
    To calculate the amount you mutliply it by the principle.
    Such that, at the end of 5 years, a principle of 1000 with a rate of 5% will have
    value = 1000 * ((100 + 5)/100)^5
    value = 1000 * 1.05 ^ 5
    value = 1276.28156


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    CuAnnan wrote: »
    You don't use document.write. You use document.innerHTML

    [PHP]
    function doSomething()
    {
    someHTML = "<br>Example</br>";
    document.getElementById("someDiv").innerHTML += someHTML;
    }
    [/PHP]

    [HTML]
    <div id="someDiv"></div>
    [/HTML]


    Is that php will that work in just js and html


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Cork24 wrote: »
    Is that php will that work in just js and html
    That's javascript


  • Registered Users Posts: 131 ✭✭CuAnnan


    Cork24 wrote: »
    Is that php will that work in just js and html

    It's JS. I was using the PHP tag to give me the relevant style.
    You can tell by the . operator. PHP uses -> as the object->method and not, as javascript, object.method


  • Advertisement
  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    Thanks for the Reply's

    I will try this when i get home.
    just looking at the code

    += someHTML;
    someHTML what do i place their ?




  • Registered Users Posts: 131 ✭✭CuAnnan


    the String representing your HTML.
    Where you had document.write(someHTML) you'd use document.getElementById('relevantId').innerHTML += someHTML;


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    Cool I'll give this a go,

    Will i need to Hide the Div Tag in my CSS so when you click on the Button the Div content would show or would it show auto.. when i click on the Button.


  • Registered Users Posts: 131 ✭✭CuAnnan


    Cork24 wrote: »
    Cool I'll give this a go,

    Will i need to Hide the Div Tag in my CSS so when you click on the Button the Div content would show or would it show auto.. when i click on the Button.
    That would depend on your initial setup.


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    No problem Thanks alot...


  • Advertisement
  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


      function playGame () {
       
       someHTML = "<br>Example</br>";
    
       document.getElementById("section").innerHTML += rollDie;
    
    
          document.close ();
    }
    
    function rollDie () {
      
          var total = 0;
        
          
               total = Math.floor((Math.random() * 6) + 1);
               total1 = Math.floor((Math.random() * 6) + 1 /1000);
    return total;
    return total1;
      }
    
    

    Having Issues with this InnerHTML


  • Registered Users Posts: 131 ✭✭CuAnnan


    Cork24 wrote: »
    function playGame () {
       
       someHTML = "<br>Example</br>";
    
        // even if rollDie were correct, this is wrong
        // the entity "rollDie" is a function.
        // setting the innerHTML to the function is not correct procedure
        // you need to set the innerHTML to the [i]return value[/i] of the function
       // which is done like so
       document.getElementById("section").innerHTML += rollDie();
    
          // you don't need to document.close when you use innerHTML
          // as you never open the document.
          //document.close ();
    }
    
    // this function is wrong
    // you can never return two values like that. You need to either stick
    // the values into an object, an array or a hashtable
    function rollDie () {
      
          var total = 0;
        
          
               total = Math.floor((Math.random() * 6) + 1);
               total1 = Math.floor((Math.random() * 6) + 1 /1000);
    return total;
    return total1;
      }
    
    

    Please see comments


  • Closed Accounts Posts: 2,663 ✭✭✭Cork24


    ok i got it Working Can i add Comments to InnerHTML

    with my Old Code its Saying 1 was rolled 20 Times 2 was Rolled 0 Times Etc..

    With Inner HTML all i am getting is
    1:4
    2:4
    3:2
    4:1
    5:1

    Etc.. i would like it the same kind a way that i had the old code.

    for (var i=0; i<20; i++) { tarr.push(rollDie()); }
    document.getElementById("dice").innerHTML = someHTML + tarr.join('<br>');


Advertisement