# SOLVED: Seconds to Human Readable Recursive Function

olayemii:

I saw a question online which required a program to convert numeric seconds into seconds, minutes, hours, days, years and i was able to come up with this function below

``var arrTime = [];function formatD(seconds){    if(seconds > 0){        if(seconds >= 1 && seconds             arrTime.push(seconds === 1 ? "1 second" : seconds + " seconds");            return arrTime;        }else        if(seconds >= 60 && seconds             arrTime.push(seconds === 60 ? "1 minute" : Math.floor(seconds/60) + " minutes");            formatD(seconds - (Math.floor(seconds/60) * 60));        }else        if(seconds >= 3600 && seconds             arrTime.push(seconds === 3600 ? "1 hour" : Math.floor(seconds/3600) + " hours");               formatD(seconds - (Math.floor(seconds/3600) * 3600))            }else        if(seconds >= 86400 && seconds             arrTime.push(seconds === 86400 ? "1 day" : Math.floor(seconds/86400) + " hours");               formatD(seconds - (Math.floor(seconds/86400) * 86400))              }else{            arrTime.push(seconds === 31536000 ? "1 year" : Math.floor(seconds/31536000) + " years");               formatD(seconds - (Math.floor(seconds/31536000) * 31536000))                    }    }}``

I have studied my code and with my basic recursion knowledge, i thought this would return an array including the result, for example if i entered

``formatD(63)``

i expected to see:

``["1 minute", "3 seconds"]``

But i keep getting no return value..any fix or something am doing wrong here?

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
HERE

This post first appeared on Stack Solved, please read the originial post: here

# Share the post

SOLVED: Seconds to Human Readable Recursive Function

×