PDA

View Full Version : Can I get the values from a form by using a for loop?

nite1x
12-06-2004, 01:37 PM
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 (users.igl.net/nite1x/golfscore.htm).
A more advanced one that asks for players names, and sends an alert with the winner's name can be viewed here (users.igl.net/nite1x/golfscore1.htm).

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?

Kor
12-06-2004, 02:06 PM
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?

<script language="JavaScript" type="text/javascript">
<!-- Begin
function getscores(f){
var Asum = 0;
var Bsum = 0;
var n = 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)"

nite1x
12-06-2004, 02:18 PM
Thanks Kor, this looks like it will work for me, will have to check it out after i get back home from work.

Nite

nite1x
12-14-2004, 12:43 AM
:thumbsup:
Kor your code works great as seen here (http://users.igl.net/nite1x/golfteamscore.htm).
;)
Thanks for your help.

Nite