Skip to main content
Participating Frequently
September 22, 2017
Question

Javascript Math.min calculations

  • September 22, 2017
  • 1 reply
  • 1285 views

My calculation will pull the lowest number if all the fields are filled in. When there is an empty field it automatically set the lowest number as zero. How do I get the calculation to read the fields that have number entered in them and ignore the empty fields.

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
September 22, 2017

It's problematic to use Math.min for that. I would create an array of all non-empty values and then use a simple function to find the lowest value in it. Something like this:

var a = this.getField("A").valueAsString;

var b = this.getField("B").valueAsString;

var c = this.getField("C").valueAsString;

var nonEmptyValues = [];

if (a!="") nonEmptyValues.push(Number(a));

if (b!="") nonEmptyValues.push(Number(b));

if (c!="") nonEmptyValues.push(Number(c));

if (nonEmptyValues.length==0) event.value = "";

else {

    nonEmptyValues.sort(numericSort);

    event.value = nonEmptyValues[0];

}

function numericSort(a,b) {

    return Number(a) - Number(b);

}

mlbooth78Author
Participating Frequently
September 22, 2017

Okay, thank you. I will give this a try.