Post

2 followers Follow
0
Avatar

Ignore empty fields in calculations

Hi there!

 

I am calculating Kg for certain fish species but run in to troubles ("The result is not a valid number") if there is an empty number field in the Kg. Here is my code and how can I tell it to ignore or treat empty fields as 0?

Thank you

var cat = @All of Peixe;
var desc = @All of T2 Kg;
var sum = 0;
for (var i = 0; i < cat.length; i++){
if(cat[i] == "Babáo"){
sum += desc [i];
}};
sum
Lara Hronn Petursdottir

Please sign in to leave a comment.

3 comments

0
Avatar

Hi Lara,

you must use the variable token @All of T2 Kg with nulls. When you enter  @All of T2 Kg  pick the token with nulls from the dropdown. You can do the same for @All of Peixe with nulls. Your code: 

var cat = @All of Peixe with nulls;
var desc = @All of T2 Kg with nulls;
var sum = 0;
for (var i = 0; i < cat.length; i++){
if(cat[i][0] == "Babáo"){
sum += Number(desc[i][0]) || 0;
}};
sum

When using "with nulls" you must add [0] behind [i]. If you are interest to know why you must use "with nulls" in this case - I've described it a bit here https://help.podio.com/hc/en-us/community/posts/222414428/comments/232268707

Rainer

Rainer Grabowski 0 votes
0
Avatar

Thank you Rainer

One more thing - how would I do:
var cat
var desc
var sum

If cat equals "Babáo" and size (new variable) "G" return sum desc 

 

Lara Hronn Petursdottir 0 votes
0
Avatar

Hey Lara,

not sure if I unerstand you right. 
Is "size" a value from the related app or is it a field in the app where the calculation happens?

If "size" is a value from related items:

var cat = @All of Peixe with nulls;
var desc = @All of T2 Kg with nulls;
var size = @All of Size with nulls;
var sum = 0;
for (var i = 0; i < cat.length; i++){
if(cat[i][0] == "Babáo" && size[i][0] == "G"){
sum += Number(desc[i][0]) || 0;
}};
sum

If "size" is a field in the same item:

var cat = @All of Peixe with nulls;
var desc = @All of T2 Kg with nulls;
var size = @Size;
var sum = 0;
for (var i = 0; i < cat.length; i++){
if(cat[i][0] == "Babáo" && size == "G"){
sum += Number(desc[i][0]) || 0;
}};
sum

Rainer

Rainer Grabowski 0 votes