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

How to determine the highest number, second highest and third?

Explorer ,
Feb 26, 2018 Feb 26, 2018

Copy link to clipboard

Copied

Good morning,

I have a form that I need the following:

The user will enter the number of bike crashes per speed

I would like the "Results" field to look at the "# Bike Crashes" and list the top three speeds in this format:

Speed 20, 55 bikes crashes, Speed 30, 61 bike crashes and Speed 25, 33 bike crashes.


What script can be used to perform this task?

Thank you in advance for your help.

TOPICS
Acrobat SDK and JavaScript , Windows

Views

395

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 26, 2018 Feb 26, 2018

Copy link to clipboard

Copied

A simple way to do this is to group the "# of crashes fields" using dot notation. For example "Crash.20", "Crash.25", etc. with this method you can extract and sort the results in a single line of code.

var aRslts = this.getField("Crash").getArray().sort(function(a,b){return b.value > a.value;}).slice(0,3);

This code returns an array of the 3 fields with the largest values. where the first field in the array has the largest value.

Formatting for output is just another step.

event.value = aRslts.map(function(a){return "Speed " + a.name.replace(/Crash\./,"") + ", " + a.value + " bike crashes";}).join("\n");

Note that this returns a multiline string, so the result field must be 3 lines in height.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Explorer ,
Feb 26, 2018 Feb 26, 2018

Copy link to clipboard

Copied

Thank you fir you r response.

Can you be more descriptive? I put the following in the "Results" fieldvar aRslts = this.getField("Crash20").getArray().sort(function(a,b){return b.value > a.value;}).slice(0,3);

Named the speed accordingly: Crash20, Crash25, Crash30, Crash35.

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 26, 2018 Feb 26, 2018

Copy link to clipboard

Copied

The script relies on the "Dot" notation. so the group name "Crash" must the separated from the speed number by a "."

This script was intended to be used in the calculation script for the Result field. However, you can test it in the Console window if you want to see how it works.

The Acrobat JavaScript Console Window - YouTube

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 26, 2018 Feb 26, 2018

Copy link to clipboard

Copied

LATEST

I would use this code as the custom calculation script of the field that should show the largest three values:

var fields = ["Crash20", "Crash25", "Crash30", "Crash35"];

var values = [];

for (var i in fields) {

    var v = this.getField(fields).valueAsString;

    if (v) values.push(Number(v));

}

values.sort(function(a,b){return Number(a) - Number(b);});

event.value = values.slice(0,3).join(", ");

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