Mode Function
Goal:
Calculate a mode amount from numbers inserted to Text Inputs in a Document or Bundled Documents.
Instructions:
- Create the Template tag Mode and attach this tag to all Text Inputs in your Template that should be calculated.
- Create the Template tag GetModeValue and attach this tag to the Text Input in your Template where the sum amount should be inserted.
- Insert the below-mentioned script to the Mode tag.
Script Example:
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()); |