Greetings, developers!
I did this Rosetta Code challenge but I still feel that nonetheless I’ve been coding everyday for months I still write too much to do some simple funcionalities and sometings write O(n²) codes knowing that probably there is an O(n log(n)) .
Im not only reducing complexity but I’m not used to think first how to use High Order Functions like map.
So, here’s my code and if you’re interested please share your resolutions.
> function convertSeconds(sec) {
> const units = [
> ["wk",604800],
> ["d",86400],
> ["hr",3600],
> ["min",60],
> ["sec",1]
> ];
> let result = [];
> let string = "";
> let index=0;
> for(index=0; sec > 0 ;index++){
> if(Math.floor(sec/units[index][1])){
> result.push(Math.floor(sec/units[index][1])+" "+units[index][0]);
> sec -= Math.floor(sec/units[index][1])*units[index][1];
> }
> }
> for(index=0;index<result.length-1;index++){
> string+=result[index] + ", " ;
> }
> string+=result[index]
> return string;
> }
>
> console.log(convertSeconds(6000000));