Quantcast
Viewing latest article 50
Browse Latest Browse All 57214

Re: Calculate Max Score & Percentage

Yes, this can be done.

 

The part of this is to name your fields to make the job easy. And the easiest way to do this is to use group naming, which means you prefix all the field names with a group name.  So the score fields would be something like "Score.Item1", "Score.Item2", etc. All the score fields are prefixed with the word "Score". 

Here's code to find the number of Score fields that are not empty. 

 

// Get a list of all the Score Feilds

var aScoreFlds = this.getField("Score").getArray();

 

// Filter into a list of Score fields that are not empty

var aFilledScore = aScoreFlds.filter(function(a){return a.value != ""});

 

// Find Max Score

var nMaxScore = aFilledScore.count * 4;

 

//Find Total Score

var nTotal = 0;

aScoreFlds.forEach(function(a){nTotal += a.value});

 

// Find Percentage

var nPercentage = nTotal/nMaxScore;

 

These calculations can be divided between a Total field and a Percentage field like this.

 

/////  Total Score field (assume it's named "TotalScore")

var aScoreFlds = this.getField("Score").getArray();

var nTotal = 0;

aScoreFlds.forEach(function(a){nTotal += Number(a.value)});

event.value - nTotal;

 

/////  Percentage field

var aScoreFlds = this.getField("Score").getArray();

var aFilledScore = aScoreFlds.filter(function(a){return a.value != ""});

var nMaxScore = aFilledScore.count * 4;

 

event.value = this.getField("TotalScore").value/nMaxScore;

Image may be NSFW.
Clik here to view.

Viewing latest article 50
Browse Latest Browse All 57214

Trending Articles