When trying to sum 3 variables it is giving 'NaN' error

anirban300
Kilo Guru

I have 3 integer variables "A", "B" & "C" which are empty and I have a forth variable "total"

I have written 3 OnChange client script on respective variables like shown below.

Now whenever I put any values in A,B or C then 'total' sums up as "NaN" unless any two variables are already filled and I fill the 3rd variable then it sums up and fill the sum in 'total'.

SCRIPT 1(On Change of variable A)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var A = parseFloat(g_form.getValue('u_strategy_score'));
var B = parseFloat(g_form.getValue('u_customer_score'));
var C = parseFloat(g_form.getValue('u_benefit_score'));

var T = A+B+C;

g_form.setValue('total', T);

}

 

SCRIPT 1(On Change of variable B)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var A = parseFloat(g_form.getValue('u_strategy_score'));
var B = parseFloat(g_form.getValue('u_customer_score'));
var C = parseFloat(g_form.getValue('u_benefit_score'));

var T = A+B+C;

g_form.setValue('total', T);

}

 

SCRIPT 1(On Change of variable C)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var A = parseFloat(g_form.getValue('u_strategy_score'));
var B = parseFloat(g_form.getValue('u_customer_score'));
var C = parseFloat(g_form.getValue('u_benefit_score'));

var T = A+B+C;

g_form.setValue('total', T);

}

 

2 REPLIES 2

Anurag Tripathi
Mega Patron
Mega Patron

If the values are integer why are'nt you using parseInt instead?

-Anurag

Abhinay Erra
Giga Sage

Add the check if the variable is filled in before you do a parseFloat

 

unction onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

var A,B,C;

if(g_form.getValue('u_strategy_score'))

A = parseFloat(g_form.getValue('u_strategy_score'));

if(g_form.getValue('u_customer_score'))
B = parseFloat(g_form.getValue('u_customer_score'));

if(g_form.getValue('u_benefit_score'))
C = parseFloat(g_form.getValue('u_benefit_score'));

var T = A+B+C;

g_form.setValue('total', T);

}