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

Can you share the actual file?