Simple script, but its not working....

Discussion in 'JavaScript' started by dkbtech, Nov 28, 2007.

  1. #1
    Hey everyone,

    I'm pretty new at JS (and programming in general), and am writing a pretty simple script to calculate poker odds. I'm using a radio button form for the user to select one of two options, and depending on the option the script calculates the value and should output it to the browser. However, nothing happens when the submit button is clicked. Its probably a pretty simple fix, but in my noobness I'm not seeing it. Here is the script and related code for the form:

    <script type = "text/javascript">
    function calculateOdds()
    {
    // Variables that will be used to calculate the pot odds
    var numberOuts = parseInt(pokerOdds.numberOuts.value), // Number of outs
    percentage; // percentage chance of hitting

    // Determine which radio button is active and calculate
    if(pokerOdds.radiobutton[1].checked)
    percentage = numberOuts / 47;
    document.write("The number of outs is " + percentage + "%.");
    else
    percentage = numberOuts / 46;
    document.write("The number of outs is " + percentage + "%.");
    }
    </script>

    <form id = "pokerOdds" action = "javascript:calculateOdds()">
    <p>Select which part of the poker hand that you wish to get the odds for:</p>

    <input type = "radio" name = "radiobutton" value = "turn" />
    <label> Odds on the turn.</label>
    <input type = "radio" name = "radiobutton" value = "river" />
    <label> Odds on the river.</label>
    <p>Enter the number of outs that you have:</p>
    <input name = "numberOuts" type = "text" />
    <p><input type = "submit" name = "submit" value = "Submit" />
    <input type = "reset" name = "reset" value = "Reset" /></p>
    </p>

    Any help would be appreciated, and thanks in advance!
     
    dkbtech, Nov 28, 2007 IP
  2. James McMurray

    James McMurray Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:

    <script>
    function calculateOdds()
    {
      // Variables that will be used to calculate the pot odds
      var numberOuts = parseInt(pokerOdds.numberOuts.value), // Number of outs
      percentage; // percentage chance of hitting
    
      // Determine which radio button is active and calculate
      if(pokerOdds.radiobutton[1].checked)
        percentage = numberOuts / 47;
      else
        percentage = numberOuts / 46;
    
      document.write("The number of outs is " + percentage + "%.");
    }</script>
    
    <form id = "pokerOdds" action = "javascript:calculateOdds()">
    <p>Select which part of the poker hand that you wish to get the odds for:</p>
    
    <input type = "radio" name = "radiobutton" value = "turn" />
    <label> Odds on the turn.</label>
    <input type = "radio" name = "radiobutton" value = "river" />
    <label> Odds on the river.</label>
    <p>Enter the number of outs that you have:</p>
    <input name = "numberOuts" type = "text" />
    <p><input type = "submit" name = "submit" value = "Submit"/>
    <input type = "reset" name = "reset" value = "Reset" /></p>
    </p>
    Code (markup):
    You didn't have brackets around the if block and else block. I moved the .write to a line of it's own.

    You might also want to have a paragraph with the id "results" and use

    document.getElementById("results").innerHtml = ("The number of outs is " + percentage + "%.");

    If you do that instead of submitting the form, they can re-enter their results without having to back up.
     
    James McMurray, Nov 28, 2007 IP
  3. dkbtech

    dkbtech Peon

    Messages:
    155
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey thanks! I didn't know about that method, but it sounds like it is a lot simpler (and more user friendly). I'll try it out.
     
    dkbtech, Nov 28, 2007 IP
  4. dkbtech

    dkbtech Peon

    Messages:
    155
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok, I made the changes and am still running into the same problem - nothing happens when "Submit" is pressed. Any other ideas of what might be going wrong?
     
    dkbtech, Nov 28, 2007 IP
  5. sharry

    sharry Peon

    Messages:
    319
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you must have missed out </form> just check
     
    sharry, Nov 28, 2007 IP
  6. dkbtech

    dkbtech Peon

    Messages:
    155
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I think you're right, I did catch that and fix it locally, and have uploaded the changes. However, when I view the source online the </form> tag isn't showing up. I'm going to try a different FTP program and see if I can figure out why its not updating.
     
    dkbtech, Nov 29, 2007 IP
  7. sharry

    sharry Peon

    Messages:
    319
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #7
    goodluck, im glad it helped
     
    sharry, Nov 30, 2007 IP