Select Page

Search Knowledge Base by Keyword

Search Knowledge Base by Keyword

Mode Function

Goal:

Calculate a mode amount from numbers inserted to Text Inputs in a Document or Bundled Documents.

Instructions:

  1. Create the Template tag Mode and attach this tag to all Text Inputs in your Template that should be calculated.
  2. Create the Template tag GetModeValue and attach this tag to the Text Input in your Template where the sum amount should be inserted.
  3. Insert the below-mentioned script to the Mode tag.

Script Example:

const Tags = LEGITO.documentBuilder.getTagsByName("Mode");

 

var finder = LEGITO.documentBuilder.event.createElementFinder();
var modeValues = finder.findElementsByTagsAuto(Tags);

 

const Results = LEGITO.documentBuilder.getTagsByName("GetModeValue");
var resultElement = finder.findElementsByTagsAuto(Results)[0];

 

let valuesArray = []
for(var i in modeValues) {
if(modeValues[i].getValue() !== null) {
valuesArray.push(parseInt(modeValues[i].getValue(), 10));
}
}

 

function mode(numbers) {
// as result can be bimodal or multi-modal,
// the returned result is provided as an array
// mode of [3, 5, 4, 4, 1, 1, 2, 3] = [1, 3, 4]
var modes = [], count = [], i, number, maxIndex = 0;
 
for (i = 0; i < numbers.length; i += 1) {
number = numbers[i];
count[number] = (count[number] || 0) + 1;
if (count[number] > maxIndex) {
maxIndex = count[number];
}
}
 
for (i in count)
if (count.hasOwnProperty(i)) {
if (count[i] === maxIndex) {
modes.push(Number(i));
}
}
 
return modes;
}

 

resultElement.setValue(mode(valuesArray).toString());