samedi 17 septembre 2016

Time function conversion output JavaScript Syntax and Approach

The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.

The resulting expression is made of components like 4 seconds, 1 year, etc. In general, a positive integer and one of the valid units of time, separated by a space. The unit of time is used in plural if the integer is greater than 1.

The components are separated by a comma and a space (", "). Except the last component, which is separated by " and ", just like it would be written in English.

A more significant units of time will occur before than a least significant one. Therefore, 1 second and 1 year is not correct, but 1 year and 1 second is.

Different components have different unit of times. So there is not repeated units like in 5 seconds and 1 second.

A component will not appear at all if its value happens to be zero. Hence, 1 minute and 0 seconds is not valid, but it should be just 1 minute.

A unit of time must be used "as much as possible". It means that the function should not return 61 seconds, but 1 minute and 1 second instead. Formally, the duration specified by of a component must not be greater than any valid more significant unit of time.

Can somebody address the best way to go about solving this without all the dirty if else statements. I am considering looping through the array resolving the outputs but I am syntax new.

function formatDuration(s2) {
var arr=[],a=s2,y1=0,m1=0,d1=0,h1=0,m2=0,s1=0,
isYear=false,
isMonth=false,
isDay=false,
isHour=false,
isMinute=false,
isSecond=true;

if(s2>=31556926)isYear=true;
if(s2>=2629743.83)isMonth=true;
if(s2>=86400)isDay=true;
if(s2>=3600)isHour=true;
if(s2>=60)isMinute=true;    

    var yearConverter=function(s){
        limit=31556926;
        if(s>=limit) 
            a=a-limit; 
            y1=y1+1;
        if(s>=limit) 
            yearConverter(a);
        else 
            monthConverter(a);
    };

    var monthConverter=function(s){
        limit=2629743.83;
        if(s>=limit)
            a=a-limit;
            m1=m1+1;        
        if(s>=limit) 
            monthConverter(a);
        else
            dayConverter(a);
    };

    var dayConverter=function(s){
        limit=86400;
        if(s>=limit)
            a=a-limit; 
            d1=d1+1;
        if(s>=limit)
            dayConverter(a);
        else
            hourConverter(a);
    };      

    var hourConverter=function(s){
        limit=3600;
        if(s>=limit)
            a=a-limit; 
            h1=h1+1;
        if(s>=limit)
            hourConverter(a);
        else
            minuteConverter(a);
    };

    var minuteConverter=function(s){
        limit=60;
        if(s>=limit)
            a=a-limit; 
            m2=m2+1;
        if(s>=limit)
            minuteConverter(a);
        else 
            secondConverter(a);
    };  

    var secondConverter=function(s){
        s1=s;
        }

    if(isYear==true)
        yearConverter(a);   
        y1=y1-1;
        arr.push(y1);

    if(isMonth == true)
        monthConverter(a);
        m1=m1-1;
        if(m1<0)
            m1=0;
            isMonth = false;
        arr.push(m1);

    if(isDay==true)
        dayConverter(a);
        d1=d1-1;
        if(d1<0)
            d1=0;
            isDay = false;
        arr.push(d1);

    if(isHour==true)
        hourConverter(a);
        h1=h1-1;
        if(h1<0)
            h1=0;
        if(h1 == 0)
            isHour = false;
        arr.push(h1);

    if(isMinute==true)
        minuteConverter(a);
        m2=m2-1;
        if(m2<0)
            m2=0;
        if(m2 <= 0)
            isMinute = false;
        arr.push(m2);

    if(isSecond==true)
        secondConverter(a); 
        if(s1 == 60)
            s1 = 0;
        if(s1 <= 0)
            isSecond = false;
        arr.push(s1);
    console.log(arr);


    if(s2 < 1)
        return 'now';

    else if(isSecond == true && isMinute,isDay,isMonth,isYear == false && arr[5] == 1)
        return arr[5]+' second';

    else if(isSecond == true && isMinute,isDay,isMonth,isYear == false && arr[5] > 1)
        return arr[5]+' seconds';

    else if(isSecond,isMinute == true && isHour,isDay,isMonth,isYear == false && arr[4]==1 && arr[5]==1 )
        return arr[4]+' minute and '+arr[5]+' second';

    else if(isSecond,isMinute == true && isHour,isDay,isMonth,isYear == false && arr[4]==1 && arr[5]>1)
        return arr[4]+' minute and '+arr[5]+' seconds';

    else if(isSecond,isHour,isDay,isYear,isMonth == false && isMinute == true && arr[4]==1 )
        return arr[4]+' minute';

    else if(isHour,isDay,isMonth,isYear,isSecond == false && isMinute == true && arr[4]>1  )
        return arr[4]+' minutes';

    else if(isSecond,isMinute == true && isHour,isDay,isMonth,isYear == false && arr[4]==1 && arr[5]>1)
        return arr[4]+' minute and '+arr[5]+' seconds'; 

    else if(isMinute == true && isHour,isDay,isMonth,isYear == false && arr[4]==1 && arr[5]==1)
        return arr[4]+' minute and '+arr[5]+' second';

    else if(isSecond,isMinute == true && arr[4]>1)
        return arr[4]+' minutes and '+arr[5]+' seconds';

    else if(isSecond,isMinute,isHour == true && arr[3]==1)
        return arr[3]+' hour '+arr[4]+' minutes and '+arr[5]+' seconds';

    else if(isSecond,isMinute,isDay == true && arr[3]>1)
        return arr[3]+' hours '+arr[4]+' seconds and '+arr[5]+' seconds';

    else if(isMinute == false && isHour,isSecond == true && arr[3]>1)
        return arr[3]+' hours and '+arr[5]+' seconds';

    else if(isSecond,isDay,isMonth,isYear == false && isMinute,isHour == true && arr[3]>1)
        return arr[3]+' hours and '+arr[3]+' minutes';

    else if(isSecond,isMinute == false && isHour == true && arr[3]>1)
        return arr[3]+' hours';

    else if(isSecond,isMinute == false && isHour == true && arr[3]==0)
        return arr[3]+' hour';

};

Aucun commentaire:

Enregistrer un commentaire