4.0 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69d03e549613fbdbe21d11e0 | Challenge 271: Medication Reminder | 28 | challenge-271 |
--description--
Given an array of medications and a string representing the current time, return the next medication you need to take and how long until you need to take it.
- Each medication is in the format
[name, lastTaken], wherenameis the name of the medication andlastTakenis the time it was last taken. - All given times will be in
"HH:MM"(24-hour) format.
Use the following medication schedule:
| Name | Schedule |
|---|---|
| Deployxitrin | 08:00, 16:00 |
| Debuggamanizole | 07:00, 13:00, 21:00 |
| Mergeflictamine | every 4 hours |
Return a string in the format "{name} in Hh Mm". For example, "Debuggamanizole in 2h 0m" or "Deployxitrin in 1h 5m".
--hints--
medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "10:00"]], "11:00") should return "Debuggamanizole in 2h 0m".
assert.equal(medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "10:00"]], "11:00"), "Debuggamanizole in 2h 0m");
medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "13:00"], ["Mergeflictamine", "14:00"]], "14:55") should return "Deployxitrin in 1h 5m".
assert.equal(medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "13:00"], ["Mergeflictamine", "14:00"]], "14:55"), "Deployxitrin in 1h 5m");
medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "13:00"], ["Mergeflictamine", "14:00"]], "17:15") should return "Mergeflictamine in 0h 45m".
assert.equal(medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "13:00"], ["Mergeflictamine", "14:00"]], "17:15"), "Mergeflictamine in 0h 45m");
medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "09:00"]], "12:59") should return "Debuggamanizole in 0h 1m".
assert.equal(medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "09:00"]], "12:59"), "Debuggamanizole in 0h 1m");
medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "21:00"], ["Mergeflictamine", "03:00"]], "06:55") should return "Debuggamanizole in 0h 5m".
assert.equal(medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "21:00"], ["Mergeflictamine", "03:00"]], "06:55"), "Debuggamanizole in 0h 5m");
medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "07:30"]], "08:00") should return "Mergeflictamine in 3h 30m".
assert.equal(medicationReminder([["Deployxitrin", "08:00"], ["Debuggamanizole", "07:00"], ["Mergeflictamine", "07:30"]], "08:00"), "Mergeflictamine in 3h 30m");
--seed--
--seed-contents--
function medicationReminder(medications, currentTime) {
return medications;
}
--solutions--
function medicationReminder(medications, currentTime) {
const schedule = {
Deployxitrin: { type: 'fixed', times: ['08:00', '16:00'] },
Debuggamanizole: { type: 'fixed', times: ['07:00', '13:00', '21:00'] },
Mergeflictamine: { type: 'interval', hours: 4 }
};
const toMinutes = t => {
const [h, m] = t.split(':').map(Number);
return h * 60 + m;
};
const current = toMinutes(currentTime);
let earliest = Infinity;
let nextMed = '';
for (const [name, lastTaken] of medications) {
const med = schedule[name];
let nextDue;
if (med.type === 'fixed') {
const futureTimes = med.times
.map(toMinutes)
.filter(t => t > current);
if (futureTimes.length === 0) continue;
nextDue = Math.min(...futureTimes);
} else {
nextDue = toMinutes(lastTaken) + med.hours * 60;
}
const diff = nextDue - current;
if (diff > 0 && diff < earliest) {
earliest = diff;
nextMed = name;
}
}
const h = Math.floor(earliest / 60);
const m = earliest % 60;
return `${nextMed} in ${h}h ${m}m`;
}