dividing two variables returns as invalid number

Comments

4 comments

  • Rainer Grabowski

    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

     

    0
    Comment actions Permalink
  • Ahmed Sakr

    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.

     

    0
    Comment actions Permalink
  • Rainer Grabowski

    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

    1
    Comment actions Permalink
  • Ahmed Sakr

    Thank You Rainer, worked!

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk