Skip to main content
Known Participant
February 27, 2023
Answered

Get the most frequent element in an array

  • February 27, 2023
  • 1 reply
  • 5469 views

I have an employee appraisal form. There are 9 objectives and each objective has 5-16 sub-objectives where the 'rater' will rate the employee for that task as Not Applicable, Not Met, Met, or Exceeded. Each objective will have it's own "Score".

 

I am looking for a javascript to look at the list of items within the objective and display the most selected option.

 

For example, Objective 1 has 6 sub-objectives.

 - The dropdown fields = Objective 1 Rating.

- The field to display the answer = Score 1.

 

 

  • 1 Not Applicable
  • 1 Not Met
  • 3 Met
  • 1 Exceeded

"Score 1" should display "Met".

 

Also, in the event that there is a tie between 2 answers I'll need some logic on that.

 

 

 

I have already tried doing if, else if statements which worked to some extent but wasn't foolproof. I also tried a switch statements but couldn't get out of a syntax error.

 

 

I just came across these scripts that will do what I've been looking for but I don't know where I put my field name. 

 

Option 1: getting a syntax error "missing ; before statement 2, line 3

function findhighestOccurenceAndNum(Objective1Rating) {
let obj = {};
let maxNum, maxVal;
for (let v of a) {
obj[v] = ++obj[v] || 1;
if (maxVal === undefined || obj[v] > maxVal) {
maxNum = v;
maxVal = obj[v];
}
}
console.log(maxNum + ' has max value = ' + maxVal);
}

findhighestOccurenceAndNum(['Not Applicable', 'Not Met', 'Met', 'Exceeded']);

 

Option 2:

 

function modeString(array) {

  if (array.length == 0) return null;

 

  var modeMap = {},

    maxEl = array[0],

    maxCount = 1;

 

  for (var i = 0; i < array.length; i++) {

    var el = array[i];

 

    if (modeMap[el] == null) modeMap[el] = 1;

    else modeMap[el]++;

 

    if (modeMap[el] > maxCount) {

      maxEl = el;

      maxCount = modeMap[el];

    } else if (modeMap[el] == maxCount) {

      maxEl += "&" + el;

      maxCount = modeMap[el];

    }

  }

  return maxEl;

This topic has been closed for replies.
Correct answer Nesa Nurani

The overall score is still giving me issues. It's not populating. Do all 9 objectives need to be filled out in order for it to populate?

 

This is what I entered.

var met = 0, Nmet = 0, NApp = 0, exc = 0;

for(var i=1; i<=9; i++){

if(this.getField("Score"+i).valueAsString == "200")met++;

if(this.getField("Score"+i).valueAsString == "100")Nmet++;

if(this.getField("Score"+i).valueAsString == "1")NApp++;

if(this.getField("Score"+i).valueAsString == "300")exc++;

var max = Math.max(met,Nmet,NApp,exc);

if(met||Nmet||NApp||exc){

if(met == max)event.value = "Met";

else if(Nmet == max)event.value = "Not Met";

else if(NApp == max)event.value = "Not Applicable";

else if(exc == max)event.value = "Exceeded";}

else event.value = "";}


Score results are strings "Met", "Not Met" they are not same as export values you get from dropdowns so change numbers to words:

if(this.getField("Score"+i).valueAsString == "Met")met++;

if(this.getField("Score"+i).valueAsString == "Not Met")Nmet++;

if(this.getField("Score"+i).valueAsString == "Not Applicable")NApp++;

if(this.getField("Score"+i).valueAsString == "Exceeded")exc++;

1 reply

Nesa Nurani
Community Expert
Community Expert
February 27, 2023

See if this works, just make sure dropdown choices and field name are correct:

var met = 0, Nmet = 0, NApp = 0, exc = 0;
for(var i=1; i<=6; i++){
if(this.getField("Objective 1."+i+" Rating").valueAsString == "Met")met++;
if(this.getField("Objective 1."+i+" Rating").valueAsString == "Not Met")Nmet++;
if(this.getField("Objective 1."+i+" Rating").valueAsString == "Not Applicable")NApp++;
if(this.getField("Objective 1."+i+" Rating").valueAsString == "Exceeded")exc++;}
var max = Math.max(met,Nmet,NApp,exc);
if(met||Nmet||NApp||exc){
if(met == max)event.value = "Met";
else if(Nmet == max)event.value = "Not Met";
else if(NApp == max)event.value = "Not Applicable";
else if(exc == max)event.value = "Exceeded";}
else event.value = "";

What do you want to happen in case of multiple choices with the same number? (2 'Met' and 2 'Not Met' for example)

KAmaral84Author
Known Participant
February 27, 2023

Hi @Nesa Nurani 

 

Thank you for your reply. When I tested your script the Score1 field remained blank the entire time. The field names all look correct.

I do have all the Tooltip labeled as just Objective 1 Rating for each sub-rating. The Name itself goes 1.1, 1.2 and so on. Does that make a difference?

 

Here is the screenshot of the results.

 

 

Here is the screenshot of field names.

 

Nesa Nurani
Community Expert
Community Expert
February 27, 2023

@Nesa Nurani 

 

I really appreciate your help. I've been trying to figure this out for too long.

 

There is one last field as well that I'm trying to script. It's the Overall Rating field. This field takes all the Scores from each objective and is to display the overall score.

 

I was using your same script but adding a line for each Score. I'm getting a syntax errror 10 at line 11 (where Score3 starts). 

 

 

var met = 0, Nmet = 0, NApp = 0, exc = 0;

for(var i=1; i<=9; i++){

if(this.getField("Score1").valueAsString == "200")met++;

if(this.getField("Score1").valueAsString == "100")Nmet++;

if(this.getField("Score1").valueAsString == "1")NApp++;

if(this.getField("Score1").valueAsString == "300")exc++;}
if(this.getField("Score2").valueAsString == "200")met++;

if(this.getField("Score2").valueAsString == "100")Nmet++;

if(this.getField("Score2").valueAsString == "1")NApp++;

if(this.getField("Score2").valueAsString == "300")exc++;}
if(this.getField("Score3").valueAsString == "200")met++;

if(this.getField("Score3").valueAsString == "100")Nmet++;

if(this.getField("Score3").valueAsString == "1")NApp++;

if(this.getField("Score3").valueAsString == "300")exc++;}
if(this.getField("Score4").valueAsString == "200")met++;

if(this.getField("Score4").valueAsString == "100")Nmet++;

if(this.getField("Score4").valueAsString == "1")NApp++;

if(this.getField("Score4").valueAsString == "300")exc++;}
if(this.getField("Score5").valueAsString == "200")met++;

if(this.getField("Score5").valueAsString == "100")Nmet++;

if(this.getField("Score5").valueAsString == "1")NApp++;

if(this.getField("Score5").valueAsString == "300")exc++;}
if(this.getField("Score6").valueAsString == "200")met++;

if(this.getField("Score6").valueAsString == "100")Nmet++;

if(this.getField("Score6").valueAsString == "1")NApp++;

if(this.getField("Score6").valueAsString == "300")exc++;}
if(this.getField("Score7").valueAsString == "200")met++;

if(this.getField("Score7").valueAsString == "100")Nmet++;

if(this.getField("Score7").valueAsString == "1")NApp++;

if(this.getField("Score7").valueAsString == "300")exc++;}
if(this.getField("Score8").valueAsString == "200")met++;

if(this.getField("Score8").valueAsString == "100")Nmet++;

if(this.getField("Score8").valueAsString == "1")NApp++;

if(this.getField("Score8").valueAsString == "300")exc++;}
if(this.getField("Score9").valueAsString == "200")met++;

if(this.getField("Score9").valueAsString == "100")Nmet++;

if(this.getField("Score9").valueAsString == "1")NApp++;

if(this.getField("Score9").valueAsString == "300")exc++;}


var max = Math.max(met,Nmet,NApp,exc);

if(met||Nmet||NApp||exc){

if(met == max)event.value = "Met";

else if(Nmet == max)event.value = "Not Met";

else if(NApp == max)event.value = "Not Applicable";

else if(exc == max)event.value = "Exceeded";}

else event.value = "";

 

 

 


If tie try this script:

var met = 0, Nmet = 0, NApp = 0, exc = 0;
var met2 = 0, Nmet2 = 0, NApp2 = 0, exc2 = 0;
for(var i=1; i<=6; i++){
if(this.getField("Objective 1."+i+" Rating").valueAsString == "200")met++;
if(this.getField("Objective 1."+i+" Rating").valueAsString == "100")Nmet++;
if(this.getField("Objective 1."+i+" Rating").valueAsString == "1")NApp++;
if(this.getField("Objective 1."+i+" Rating").valueAsString == "300")exc++;}
var max = Math.max(met,Nmet,NApp,exc);
if(max != 0){
if(NApp == max)NApp2 = 1;
if(Nmet == max)Nmet2 = 2;
if(met == max)met2 = 3;
if(exc == max)exc2 = 4;}
var max2 = Math.max(met2,Nmet2,NApp2,exc2);
if(max2 == 1)event.value = "Not Applicable";
else if(max2 == 2)event.value = "Not Met";
else if(max2 == 3)event.value = "Met";
else if(max2 == 4)event.value = "Exceeded";
else
event.value = "";

For the overall rating field, loop takes care of all fields 1-9, so there is no need to write all of them. Delete all 'if' lines and replace them with this:

if(this.getField("Score"+i).valueAsString == "200")met++;
if(this.getField("Score"+i).valueAsString == "100")Nmet++;
if(this.getField("Score"+i).valueAsString == "1")NApp++;
if(this.getField("Score"+i).valueAsString == "300")exc++;