Removing Blank Cells In A Table Row Prior To Performing Calculation?

I am the caretaker of our disc golf league’s weekly scoring system. Player handicaps are calculated each week based on a simple formula that involves the current score plus 4 previous scores. In order to establish a handicap, a player must have a minimum of 3 scores. The online system is written in PHP interacting with a MySQL database. The system lets the players see their current handicaps. (I’ve been writing PHP code for about 20 years).

I’m an old dog trying to learn new tricks. In this case . . . Javascript. I wanted to allow players to see their handicaps at different points throughout the season. Instead of doing this with PHP, I decided to see if it could be done with Javascript. In the first iteration, I generated a grid of players and scores (using PHP/MySQL) showing the players along the left and the times each player played across the top. When the mouse arrow is hovered over any player score, the Javascript code calculates the handicap based on the hovered score and the four scores to its left. No handicap is calculated for the first two scores in a row:

Disc Golf Scores and Handicaps (view source)

In a second iteration, I decided to generate a grid with the players on the left and the event dates across the top. This produces blank cells in almost all of the rows. The blank cells have been a major stumbling block in trying to get the Javascript to do what I want it to do. Here’s the second iteration:

EDGE Scoring Grid (view source)

I am using the same Javascript code for both of these (even though I’ve spend hours and hours researching and modifying the second one . . . with no luck).

I’m hoping that the collective expertise of this group can provide guidance to enlighten a novice as to how to get Javascript to either (1) ignore the empty cells in a row prior to adding values to the array or (2) take all the values in a row, move the non-zero values to the left, then remove the empty (NULL) values and assign what’s left to the array or (3) something else that I can’t envision.

Thanks in advance for taking a look.
-MD

I’m pleased to report that mabismad was able to provide a very simple and elegant solution over at the Sitepoint forum:

Because the current code putting values into the scores array is removing non-numbers, and you are only using the last 5 scores in the calculateHandicap() function, all you really need to do is change the for() loop, from starting at index - 4, to starting at 1. Note: the player’s name is index = 0, so you can skip it in all the loops/conditional tests…

See if using the following for the for() loop produces the result you expect -

  		for (let i = 1; i <= index; i++) {

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.