• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Get the most frequent element in an array

New Here ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

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.

 

Picture2.27.pngexpand image

 

 

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;

TOPICS
How to , JavaScript , PDF forms

Views

4.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
2 ACCEPTED SOLUTIONS
Community Expert ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

On some choices, you have an export value also you have changed field names in script, try now:

var met = 0, Nmet = 0, NApp = 0, exc = 0;
for(var i=1; i<=6; i++){
if(this.getField("Objective 1."+i+" Rating").valueAsString == "100")met++;
if(this.getField("Objective 1."+i+" Rating").valueAsString == "1")Nmet++;
if(this.getField("Objective 1."+i+" Rating").valueAsString == "Not Applicable")NApp++;
if(this.getField("Objective 1."+i+" Rating").valueAsString == "200")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 = "";

You have other errors in your file from previous scripts.

View solution in original post

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

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++;

View solution in original post

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

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?

KAmaral84_2-1677527742959.pngexpand image

 

Here is the screenshot of the results.

 

KAmaral84_0-1677527686251.pngexpand image

 

Here is the screenshot of field names.

KAmaral84_1-1677527704720.pngexpand image

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

Can you share the actual file?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

On some choices, you have an export value also you have changed field names in script, try now:

var met = 0, Nmet = 0, NApp = 0, exc = 0;
for(var i=1; i<=6; i++){
if(this.getField("Objective 1."+i+" Rating").valueAsString == "100")met++;
if(this.getField("Objective 1."+i+" Rating").valueAsString == "1")Nmet++;
if(this.getField("Objective 1."+i+" Rating").valueAsString == "Not Applicable")NApp++;
if(this.getField("Objective 1."+i+" Rating").valueAsString == "200")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 = "";

You have other errors in your file from previous scripts.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

That is great! Thank you so much!

 

Three more questions please,

 

1) My client wants Not Applicable to have a value so I changed the values of the options to

1 = Not Applicable

100 = Not Met

200 = Met

300 = Exceeded. I made these changes (bolded). Can you review and make sure that is 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 == "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(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 = "";

 

2) When I apply this to the other objectives am I changing the second line (for(var i=1; i<=6; i++){)? For example, Objective 4 has 16 sub-objectives. Would I change the part I bolded?  var i=1; i<=16; i++

 

3) If there is a tie between two values, can you include a line to tell it to grab the higher valued rating?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

1. Looks ok.

2. Yes.

3. It's very late here, if someone else doesn't help in the meantime I'll look at it in the morning.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

@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 = "";

 

 

KAmaral84_0-1677534440704.pngexpand image

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

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++;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

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 = "";}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

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++;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 28, 2023 Feb 28, 2023

Copy link to clipboard

Copied

LATEST

Can't thank you enough @Nesa Nurani.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines