Post

3 followers Follow
0
Avatar

dividing two variables returns as invalid number

I have this in a calculation filed in order to calculate the number of successful jobs completed as a percentage to total jobs. When I get at the end the var jobperc=(completevalue / totaljobs)*100; returns that it is not a valid number. I have replaced the variables with numbers and it works fine. I need help because I am stumped

 

var workstat = @All of Work Status;
var jobcomplete = 0;
var joblate = 0;
var jobfailed = 0;
var totaljobs = 0;

for (var i = 0; i< workstat.length; i++)
{
if (workstat[i]=="4- Accepted")
{
jobcomplete=jobcomplete + 1;
}

if (workstat[i]=="5- Accepted Late")
{
joblate=joblate + 1;
}

if (workstat[i]=="6- Failed")
{
jobfailed=jobfailed + 1;
}

if (workstat[i]=="6- Failed" || workstat[i]=="4- Accepted" || workstat[i]=="5- Accepted Late")
{
totaljobs=totaljobs + 1;
}
}


var completevalue=jobcomplete+(joblate*.5)-jobfailed;
var jobperc=(completevalue / totaljobs)*100;


jobperc

Faruq Hunter

Please sign in to leave a comment.

4 comments

0
Avatar

Hi Faruq,

you get "not a valid number" cause your variable totaljobs is always 0 - and Javascript can't divide by 0.
The IF sequence stops when one condition is true - so the last IF can never be true.

var workstat = @All of Work Status;
var jobcomplete = 0;
var joblate = 0;
var jobfailed = 0;
for (var i = 0; i< workstat.length; i++)
{
if (workstat[i]=="4- Accepted")
{
jobcomplete += 1
}
if (workstat[i]=="5- Accepted Late")
{
joblate += 1; 
}
if (workstat[i]=="6- Failed") 
{
jobfailed += 1; 
}
};
var totaljobs = jobcomplete + joblate + jobfailed;
var totaljobs = totaljobs == 0 ? 1 : totaljobs;
var completevalue = jobcomplete+(joblate*.5)-jobfailed;
var jobperc = (completevalue / totaljobs)*100;
jobperc

Rainer

 

Rainer Grabowski 0 votes
Comment actions Permalink
0
Avatar

Hello Rainer

I have this calculation in the below picture that returns "not a valid number" error and I have no clue what is wrong, I've tried to create an array with push() and then divide by array.length but I'm getting the same error also.

 

Ahmed Sakr 0 votes
Comment actions Permalink
1
Avatar

Hello Ahmed,

modify the var DevRate in the loop:

DevRate += Number(@All of Development Rate[i]) || 0;

And the last line: 

count > 0  DevRate/count : 0

Javascript can't divide by 0. 

Rainer

Rainer Grabowski 1 vote
Comment actions Permalink