Back to Freecodecamp

Challenge 233: Wake-Up Alarm

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69b1028d6e265413d0198a2c.md

latest1.9 KB
Original Source

--description--

Given a string representing the time you set your alarm and a string representing the time you actually woke up, determine if you woke up early, on time, or late.

  • Both times will be given in "HH:MM" 24-hour format.

Return:

  • "early" if you woke up before your alarm time.
  • "on time" if you woke up at your alarm time, or within the 10 minute snooze window after the alarm time.
  • "late" if you woke up more than 10 minutes after your alarm time.

Both times are on the same day.

--hints--

alarmCheck("07:00", "06:45") should return "early".

js
assert.equal(alarmCheck("07:00", "06:45"), "early");

alarmCheck("06:30", "06:30") should return "on time".

js
assert.equal(alarmCheck("06:30", "06:30"), "on time");

alarmCheck("08:10", "08:15") should return "on time".

js
assert.equal(alarmCheck("08:10", "08:15"), "on time");

alarmCheck("09:30", "09:45") should return "late".

js
assert.equal(alarmCheck("09:30", "09:45"), "late");

alarmCheck("08:15", "08:25") should return "on time".

js
assert.equal(alarmCheck("08:15", "08:25"), "on time");

alarmCheck("05:45", "05:56") should return "late".

js
assert.equal(alarmCheck("05:45", "05:56"), "late");

alarmCheck("04:30", "04:00") should return "early".

js
assert.equal(alarmCheck("04:30", "04:00"), "early");

--seed--

--seed-contents--

js
function alarmCheck(alarmTime, wakeTime) {

  return alarmTime;
}

--solutions--

js
function alarmCheck(alarmTime, wakeTime) {
  const [alarmH, alarmM] = alarmTime.split(":").map(Number);
  const [wakeH, wakeM] = wakeTime.split(":").map(Number);

  const alarmMinutes = alarmH * 60 + alarmM;
  const wakeMinutes = wakeH * 60 + wakeM;
  const diff = wakeMinutes - alarmMinutes;

  if (diff < 0) return "early";
  if (diff <= 10) return "on time";
  return "late";
}