Soren Winslow
Golf Handicap Index Calculator
This is a fairly simple script that will calculate your golf handicap index according to the USGA's formula.
Each round of golf is first calculated with the USGA's formula of (Score - Course Rating) * 113 /Slope Rating. Depending on the number of rounds entered, a certian number of lowest rounds are then dropped. An average of the remaining differentials is calculated. The average is then multiplied by .96 to get the final handicap index.
Here is how the code works.
When the form is submitted there will be a comma delimited list for each of the form fields and an array will be created for each of the neccesary values for the formula.
Each matching set of values will be calculated and the results will be stored into an array.
To accomodate the number of values that are dropped, the array will then be sorted according to value and ran through a loop stopping at the appropriate number of values. During that loop, the values will be added together.
The sum of the values will be then averages (divided by the number of values) and then multiplied by .96 to get the resulting golf handicap index value.
Normally a minimum of five rounds of 18 holes of golf is required to get an accurate handicap index, but for the sake of demonstration and to give you a general idea of your handicap index, only one is required.

Round Score Rating Slope
Here is the ASP code and the JavaScript code for this process:

<%



  If Request("DoStuff") = "Calculate Handicap" Then



     Sub SortArray(TheArr,AscDesc)

      TempVal = ""

      For x = 0 to UBound(TheArr)



         For y = x+1 to UBound(TheArr)

            If AscDesc = "Desc" Then

              If Cdbl(TheArr(x)) < CDbl(TheArr(y)) then

                 TempVal = TheArr(y)

                 TheArr(y) = TheArr(x)

                 TheArr(x) = TempVal

              End If

            Else

              If Cdbl(TheArr(x)) > Cdbl(TheArr(y)) then

                 TempVal = TheArr(x)

                 TheArr(x) = TheArr(y)

                 TheArr(y) = TempVal

              End If

            End If

         Next



      Next



     End Sub



     dim DiffArr()

     RoundNumArr = Split(Request("RoundNum"),",")

     ScoreArr = Split(Request("Score"),",")

     RatingArr = Split(Request("Rating"),",")

     SlopeArr = Split(Request("Slope"),",")

     NumRounds = UBound(RoundNumArr)



     For x = 0 To UBound(RoundNumArr)-1

       ReDim Preserve DiffArr(x)

       DiffArr(x) = CDbl((ScoreArr(x)-RatingArr(x))*113/SlopeArr(x)) & ","

     Next



     Call SortArray(DiffArr,"Desc")



     If NumRounds < 5 Then

     ElseIf NumRounds <= 6 Then

        NumRounds = NumRounds - 1

     ElseIf NumRounds <= 8 Then

         NumRounds = NumRounds - 2

     ElseIf NumRounds <= 10 Then

         NumRounds = NumRounds - 3

     ElseIf NumRounds <= 12 Then

         NumRounds = NumRounds - 4

     ElseIf NumRounds <= 14 Then

         NumRounds = NumRounds - 5

     ElseIf NumRounds <= 16 Then

         NumRounds = NumRounds - 6

     ElseIf NumRounds = 17 Then

         NumRounds = NumRounds - 7

     ElseIf NumRounds = 18 Then

         NumRounds = NumRounds - 8

     ElseIf NumRounds = 19 Then

         NumRounds = NumRounds - 9

     ElseIf NumRounds = 20 Then

         NumRounds = NumRounds - 10

     End If



     DiffTotal = 0

     For x = 1 To NumRounds

         DiffTotal = DiffTotal + CDbl(DiffArr(x-1))

     Next



     DiffAvg = DiffTotal/NumRounds





     HandicapTotal = FormatNumber(DiffAvg*.96,2)



     Response.Write "<div class=" & chr(34) & "breg" & chr(34) & " " & _

                    "style=" & chr(34) & "text-align:center;font-weight:bold;padding:3px;" & chr(34) & ">" & _

                    "Your Golf Handicap Index is " & HandicapTotal & _

                    "</div>"





  End If

%>



 <form name="GolfInfoForm" method="post" action="GolfHandicap.asp" onsubmit="return ValidateForm();">

 <table name="GolfInfoTbl" align="center" class="DataCell" cellspacing="0" style="background-color:#ffffff;">



 <thead>

 <tr>

 <td class="DataCellHead" title="Round Number">Round</td>

 <td class="DataCellHead" title="Your Score For The Round">Score</td>

 <td class="DataCellHead" title="The Course Rating">Rating</td>

 <td class="DataCellHead" title="The Slope Rating">Slope</td>

 <td class="DataCellHead">

         <input type="button" class="SubButton2" value="AddRow" onclick="moreFields();">

 </td>

 </tr>

 </thead>



 <tbody id="InfoRow" style="display:none;">

 <tr style="text-align:center;">

 <td class="DataCell"><input type="text" name="RoundNum" value="0" size="1" class="breg"></td>

 <td class="DataCell"><input type="text" name="Score" value="0" size="3" class="breg"></td>

 <td class="DataCell"><input type="text" name="Rating" value="0" size="3" class="breg"></td>

 <td class="DataCell"><input type="text" name="Slope" value="0" size="3" class="breg"></td>

 <td class="SubButton" onclick="this.parentNode.parentNode.removeChild(this.parentNode);ReNumber();">

     Remove

 </td>

 </tr>

 </tbody>



 <tfoot>

 <tr>

 <td colspan="5" class="DataCell" style="text-align:right;">

     <input type="submit" name="DoStuff" class="SubButton" value="Calculate Handicap">

 </tr>

 </tfoot>



 </table>

 </form>





<script language="JavaScript">



var counter = 0;

var TheForm = document.forms['GolfInfoForm'];

function moreFields()

{

  counter++;

  if(counter<20)

  {

    var newFields = document.getElementById("InfoRow").cloneNode(true);

    var insertHere = document.getElementById("InfoRow");

    newFields.id = "InfoRow" + counter;

  	newFields.style.display = "block";

    var newField = newFields.childNodes;

    insertHere.parentNode.insertBefore(newFields,insertHere);

  	TheForm.RoundNum[counter].value = counter;

  }

  ReNumber();

}



function ReNumber()

{

 for (var x=0;x<TheForm.RoundNum.length;x++)

   {

    TheForm.RoundNum[x].value = x;

    if(navigator.appName == "Microsoft Internet Explorer")

      {

       TheForm.RoundNum[x].value = x+1;

      }

     counter = x;

   }

}







<%

If Request("DoStuff") = "Calculate Handicap" Then



   For x = 0 to UBound(RoundNumArr)-1

      Response.Write "moreFields();" & chr(13)

      Response.Write "TheForm.RoundNum[" & x & "].value = " & x & ";" & chr(13)

      Response.Write "TheForm.Score[" & x & "].value = " & ScoreArr(x) & ";" & chr(13)

      Response.Write "TheForm.Rating[" & x & "].value = " & RatingArr(x) & ";" & chr(13)

      Response.Write "TheForm.Slope[" & x & "].value = " & SlopeArr(x) & ";" & chr(13)

   Next



   RoundNumArr = Split(Request("RoundNum"),",")

   ScoreArr = Split(Request("Score"),",")

   RatingArr = Split(Request("Rating"),",")

   SlopeArr = Split(Request("Slope"),",")

   NumRounds = UBound(RoundNumArr)

Else

      Response.Write "moreFields();" & chr(13)

End If

%>



function ValidateForm()

{

 var IsErr = false;

 var TheErr = "";

 var x = 1;

 var NumRows = TheForm.RoundNum.length;

 if(navigator.appName == "Microsoft Internet Explorer")

   {

    x = 0;

    NumRows = NumRows - 1

   }



  for (x=x;x<NumRows;x++)

   {

    var TheRowNum = x;

    if(navigator.appName == "Microsoft Internet Explorer")

      {

       TheRowNum = x+1

      }



    if (TheForm.Score[x].value.length == 0 || isNaN(TheForm.Score[x].value) == true)

     {

      IsErr = true;

      TheErr = TheErr + "\n\nPlease enter your score for round " + x;

     }

    else if (parseFloat(TheForm.Score[x].value) <= 0)

     {

      IsErr = true;

      TheErr = TheErr + "\n\nPlease enter your score for round " + x;

     }



    if (TheForm.Rating[x].value.length == 0 || isNaN(TheForm.Rating[x].value) == true)

     {

      IsErr = true;

      TheErr = TheErr + "\n\nPlease enter the course rating for round " + x;

     }

    else if (parseFloat(TheForm.Rating[x].value) <= 0)

     {

      IsErr = true;

      TheErr = TheErr + "\n\nPlease enter the course rating for round " + x;

     }

    if (TheForm.Slope[x].value.length == 0 || isNaN(TheForm.Slope[x].value) == true)

     {

      IsErr = true;

      TheErr = TheErr + "\n\nPlease enter the slope rating for round " + x;

     }

    else if (parseFloat(TheForm.Slope[x].value) <= 0)

     {

      IsErr = true;

      TheErr = TheErr + "\n\nPlease enter the slope rating for round " + x;

     }

   }



   if (IsErr == true)

   {

    alert(TheErr);

    return false;

   }

   else

   {

    TheForm.submit();

   }

}



</script>

               
© 1967 - 2017 Soren Winslow