### re: Daily Challenge #21 - Human Readable Time VIEW POST

Javascript (no idea whether this works or not :D )

const quoAndRem = (divd,div) => {
let rem = divd % div
let quo = (divd - rem)/div

return [quo,rem]
}

const formatter = (...vals) => {
const output = vals.map((itm,indx) => {
let unit
switch (indx) {
case 0:
unit = "year"
break;
case 1:
unit = "day"
break;
case 2:
unit = "hour"
break;
case 3:
unit = "minute"
break;
case 4:
unit = "second"
break;
}

if (indx !== 1) {
unit += "s"
}

return [itm,unit]
}).filter(itm => itm[0] !== 0).reduce((accm,curr,indx,arr) => {
if (indx < arr.length - 2) {
return ${accm}${curr[0]} ${curr[1]},  } else if (indx === arr.length - 2) { return ${accm}${curr[0]}${curr[1]} and
} else {
return ${accm}${curr[0]} \${curr[1]}
}
},"");

return output
}

const yearInSecs = 365*24*60*60
const dayInSecs = 24*60*60
const hourInSecs = 60*60
const minInSecs = 60
const secInSecs = 1

let yearVal = quoAndRem(seconds, yearInSecs)
let dayVal = quoAndRem(yearVal[1], dayInSecs)
let hourVal = quoAndRem(dayVal[1], hourInSecs)
let minVal = quoAndRem(hourVal[1], minInSecs)
let secVal = quoAndRem(minVal[1], secInSecs)

output = formatter(yearVal[0],dayVal[0],hourVal[0],minVal[0],secVal[0])

return output
}



I may change the formatter function to be more elegant and less crappy using slice and arrays and idontknow.

Code of Conduct Report abuse