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
-
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;
jobpercRainer
Please sign in to leave a comment.
Comments
4 comments