Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2004
    Location
    Ontario, Canada
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can I get the values from a form by using a for loop?

    I've made an "interactive" scorecard for a simulated golf game where the players can enter their scores in a form, click a button and get the totals.

    The basic scorecard can be viewed here.
    A more advanced one that asks for players names, and sends an alert with the winner's name can be viewed here.

    I would like to make a scorecard for team play with two teams of two players per team. I want to be able to take the best (lowest) score per team for each hole and add them to find the total score. To do this with the same method means at least 72 lines of coding with all the possible errors entailed. Does anyone know of a way I can use a for loop to get the values from the form, and add only the lowest score for each team to get a total score?

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    a way I can use a for loop
    I rewrote your code using a for loop. Do you think that you can manage further to do whiever comparation you wanna do?

    PHP Code:
    <script language="JavaScript" type="text/javascript">
    <!-- 
    Begin
    function getscores(f){
    var 
    Asum 0;
    var 
    Bsum 0;
    var 
    18;//number of holes
    for (var i=0;i<n;i++){
    Asum Asum+f.elements['pA'+(i+1)].value*1;
    Bsum Bsum+f.elements['pB'+(i+1)].value*1;
    }
    f.elements['pAtotal'].value=Asum;
    f.elements['pBtotal'].value=Bsum;
    if (
    Asum Bsum){
    alert (player1 "  WINS")
    }
    else if(
    Asum Bsum){
    alert(player2 "  WINS")
    }
    else{
    alert ("IT IS A TIE")}
    }
    //  End -->
    </script> 
    Note that I used a parameter to pass the form a variable, Thus don't forget to replace also in button

    onclick="getscores()"

    with

    onclick="getscores(this.form)"
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Dec 2004
    Location
    Ontario, Canada
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Kor, this looks like it will work for me, will have to check it out after i get back home from work.

    Nite

  • #4
    New Coder
    Join Date
    Dec 2004
    Location
    Ontario, Canada
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Kor your code works great as seen here.

    Thanks for your help.

    Nite


  •  

    Posting Permissions

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