2.3 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6a26df3e2988bcdded204897 | Challenge 361: Spoken Time | 28 | challenge-361 |
--description--
Given the angles for the hour and minute hands of an analog clock in degrees (clockwise from 12), return the time in spoken English.
Convert the minute hand angle to minutes (360° = 60 minutes), then use the following rules:
| Minutes | Spoken |
|---|---|
| 0 | "Y o'clock" |
| 15 | "quarter past Y" |
| 1–29 (excluding 15) | "X minutes past Y" |
| 30 | "half past Y" |
| 45 | "quarter to Z" |
| 31–59 (excluding 45) | "X minutes to Z" (where X is 60 - minutes) |
Where Y is the current hour and Z is the next hour, both derived from the hour hand angle (360° = 12 hours).
Note: Hand angles may not land exactly on a number, consider rounding them somehow.
--hints--
getSpokenTime(90, 0) should return "3 o'clock".
assert.equal(getSpokenTime(90, 0), "3 o'clock");
getSpokenTime(160, 120) should return "20 minutes past 5".
assert.equal(getSpokenTime(160, 120), "20 minutes past 5");
getSpokenTime(255, 180) should return "half past 8".
assert.equal(getSpokenTime(255, 180), "half past 8");
getSpokenTime(67.5, 92) should return "quarter past 2".
assert.equal(getSpokenTime(67.5, 92), "quarter past 2");
getSpokenTime(200, 240) should return "20 minutes to 7".
assert.equal(getSpokenTime(200, 240), "20 minutes to 7");
getSpokenTime(322.5, 273) should return "quarter to 11".
assert.equal(getSpokenTime(322.5, 273), "quarter to 11");
getSpokenTime(117.5, 335) should return "5 minutes to 4".
assert.equal(getSpokenTime(117.5, 335), "5 minutes to 4");
--seed--
--seed-contents--
function getSpokenTime(hourAngle, minuteAngle) {
return hourAngle;
}
--solutions--
function getSpokenTime(hourAngle, minuteAngle) {
const minutes = Math.floor(minuteAngle / 6);
const hour = Math.floor(hourAngle / 30) || 12;
const nextHour = (hour % 12) + 1;
if (minutes === 0) return `${hour} o'clock`;
if (minutes === 15) return `quarter past ${hour}`;
if (minutes === 30) return `half past ${hour}`;
if (minutes === 45) return `quarter to ${nextHour}`;
if (minutes < 30) return `${minutes} minutes past ${hour}`;
return `${60 - minutes} minutes to ${nextHour}`;
}