dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
2.2 KiB
2.2 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6a26df3e2988bcdded204896 | Challenge 360: Spoken Duration | 28 | challenge-360 |
--description--
Given a number of seconds, return the duration in spoken English.
- Break the duration into hours, minutes, and seconds.
- Skip any zero values.
- Use singular or plural as appropriate (
"1 hour","2 hours"). - If present, join the last two units with
"and", and the second and third to last units with a comma ("1 hour, 2 minutes and 3 seconds").
--hints--
getSpokenDuration(3723) should return "1 hour, 2 minutes and 3 seconds".
assert.equal(getSpokenDuration(3723), "1 hour, 2 minutes and 3 seconds");
getSpokenDuration(7295) should return "2 hours, 1 minute and 35 seconds".
assert.equal(getSpokenDuration(7295), "2 hours, 1 minute and 35 seconds");
getSpokenDuration(8521) should return "2 hours, 22 minutes and 1 second".
assert.equal(getSpokenDuration(8521), "2 hours, 22 minutes and 1 second");
getSpokenDuration(435) should return "7 minutes and 15 seconds".
assert.equal(getSpokenDuration(435), "7 minutes and 15 seconds");
getSpokenDuration(14455) should return "4 hours and 55 seconds".
assert.equal(getSpokenDuration(14455), "4 hours and 55 seconds");
getSpokenDuration(72000) should return "20 hours".
assert.equal(getSpokenDuration(72000), "20 hours");
getSpokenDuration(1) should return "1 second".
assert.equal(getSpokenDuration(1), "1 second");
--seed--
--seed-contents--
function getSpokenDuration(seconds) {
return seconds;
}
--solutions--
function getSpokenDuration(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;
const parts = [];
if (hours) parts.push(`${hours} ${hours === 1 ? "hour" : "hours"}`);
if (minutes) parts.push(`${minutes} ${minutes === 1 ? "minute" : "minutes"}`);
if (secs) parts.push(`${secs} ${secs === 1 ? "second" : "seconds"}`);
if (parts.length === 1) return parts[0];
if (parts.length === 2) return `${parts[0]} and ${parts[1]}`;
return `${parts[0]}, ${parts[1]} and ${parts[2]}`;
}