Untitled
unknown
plain_text
4 years ago
1.6 kB
8
Indexable
/*
* Complete the 'timeInWords' function below.
*
* The function is expected to return a STRING.
* The function accepts following parameters:
* 1. INTEGER h
* 2. INTEGER m
*/
/* This could be a dictionary imported from a locale or so */
const _partialNames = new Map([[1, "one"], [2, "two"], [3, "three"], [4, "four"], [5, "five"], [6, "six"], [7, "seven"], [8, "eight"], [9, "nine"], [10, "ten"], [11, "eleven"], [12, "twelve"], [13, "thirteen"], [15, "fifteen"], [20, "twenty"], [30, "thirty"], [40, "forty"], [50, "fifty"]]);
function timeInWords(h, m) {
const isQuarter = (i) => !!i % 15
const convert = (i) => {
if (_partialNames.has(i)) {
return _partialNames.get(i)
}
let smallDigit = i % 10
let largeDigit = i - smallDigit
if (largeDigit === 10) {
return `${_partialNames.get(smallDigit)}teen`
}
return `${_partialNames.get(largeDigit)} ${_partialNames.get(smallDigit)}`
}
const pluralize = (w, v) => v > 1 ? w + "s" : w
if (m === 0) {
return `${convert(h)} o' clock`
} else if (m === 15) {
return `quarter past ${convert(h)}`
} else if (m === 30) {
return `half past ${convert(h)}`
} else if (m === 45) {
return `quarter to ${convert((h + 1) % 12)}`
} else if (m < 30) {
return `${convert(m)} ${pluralize("minute", m)} past ${convert(h)}`
} else if (m > 30) {
return `${convert(60 - m)} ${pluralize("minute", m)} to ${convert((h + 1) % 12)
} `
}
}
Editor is loading...