When trying to sum 3 variables it is giving 'NaN' error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2018 08:00 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2018 08:06 AM
If the values are integer why are'nt you using parseInt instead?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2018 08:13 AM
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);
}