Created
January 8, 2017 11:02
-
-
Save railsstudent/5753ffbc274d23c82f5e6ab43b51a6b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function buildString(value, unit) { | |
if (value > 0) { | |
return value + ' ' + unit + (value > 1 ? 's' : '') + ', '; | |
} | |
return ''; | |
} | |
function formatDuration (seconds) { | |
// Complete this function | |
if (seconds === 0) { | |
return "now"; | |
} | |
var tmpSeconds = seconds; | |
const NUM_SEC_PER_MIN = 60; | |
const NUM_SEC_PER_HR = NUM_SEC_PER_MIN * 60; | |
const NUM_SEC_PER_DAY = NUM_SEC_PER_HR * 24; | |
const NUM_SEC_PER_YEAR = NUM_SEC_PER_DAY * 365; | |
var numYears = Math.floor(tmpSeconds / NUM_SEC_PER_YEAR); | |
tmpSeconds -= numYears * NUM_SEC_PER_YEAR; | |
var numDays = Math.floor(tmpSeconds / NUM_SEC_PER_DAY); | |
tmpSeconds -= numDays * NUM_SEC_PER_DAY; | |
var numHrs = Math.floor(tmpSeconds / NUM_SEC_PER_HR); | |
tmpSeconds -= numHrs * NUM_SEC_PER_HR; | |
var numMins = Math.floor(tmpSeconds / NUM_SEC_PER_MIN); | |
var numSecs = tmpSeconds - numMins * NUM_SEC_PER_MIN; | |
var formattedStr = buildString(numYears, 'year'); | |
formattedStr += buildString(numDays, 'day'); | |
formattedStr += buildString(numHrs, 'hour'); | |
formattedStr += buildString(numMins, 'minute'); | |
formattedStr += buildString(numSecs, 'second'); | |
// trim last one | |
var idx = formattedStr.lastIndexOf(', '); | |
formattedStr = formattedStr.substring(0, idx); | |
// replace the second last , with and | |
idx = formattedStr.lastIndexOf(', '); | |
if (idx > 0) { | |
formattedStr = formattedStr.substring(0, idx) + ' and ' + formattedStr.substring(idx + 2); | |
} | |
return formattedStr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment