Back to Freecodecamp

Challenge 175: Digital Detox

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69738771fb5a7b8b24cca2a1.md

latest2.9 KB
Original Source

--description--

Given an array of your login logs, determine whether you have met your digital detox goal.

Each log is a string in the format "YYYY-MM-DD HH:mm:ss".

You have met your digital detox goal if both of the following statements are true:

  • You logged in no more than once within any four-hour period.
  • You logged in no more than 2 times on any single day.

--hints--

digitalDetox(["2026-02-01 08:00:00", "2026-02-01 12:30:00"]) should return true.

js
assert.isTrue(digitalDetox(["2026-02-01 08:00:00", "2026-02-01 12:30:00"]));

digitalDetox(["2026-02-01 04:00:00", "2026-02-01 07:30:00"]) should return false.

js
assert.isFalse(digitalDetox(["2026-02-01 04:00:00", "2026-02-01 07:30:00"]));

digitalDetox(["2026-01-31 08:21:30", "2026-01-31 14:30:00", "2026-02-01 08:00:00", "2026-02-01 12:30:00"]) should return true.

js
assert.isTrue(digitalDetox(["2026-01-31 08:21:30", "2026-01-31 14:30:00", "2026-02-01 08:00:00", "2026-02-01 12:30:00"]));

digitalDetox(["2026-01-31 10:40:21", "2026-01-31 15:19:41", "2026-01-31 21:49:50", "2026-02-01 09:30:00"]) should return false.

js
assert.isFalse(digitalDetox(["2026-01-31 10:40:21", "2026-01-31 15:19:41", "2026-01-31 21:49:50", "2026-02-01 09:30:00"]));

digitalDetox(["2026-02-05 10:00:00", "2026-02-01 09:00:00", "2026-02-03 22:15:00", "2026-02-02 12:10:00", "2026-02-02 07:15:00", "2026-02-04 09:45:00", "2026-02-01 16:50:00", "2026-02-03 09:30:00"]) should return true.

js
assert.isTrue(digitalDetox(["2026-02-05 10:00:00", "2026-02-01 09:00:00", "2026-02-03 20:15:00", "2026-02-02 12:10:00", "2026-02-02 07:15:00", "2026-02-04 09:45:00", "2026-02-01 16:50:00", "2026-02-03 09:30:00"]));

digitalDetox(["2026-02-05 10:00:00", "2026-02-01 09:00:00", "2026-02-03 22:15:00", "2026-02-02 12:10:00", "2026-02-02 07:15:00", "2026-02-04 01:45:00", "2026-02-01 16:50:00", "2026-02-03 09:30:00"]) should return false.

js
assert.isFalse(digitalDetox(["2026-02-05 10:00:00", "2026-02-01 09:00:00", "2026-02-03 22:15:00", "2026-02-02 12:10:00", "2026-02-02 07:15:00", "2026-02-04 01:45:00", "2026-02-01 16:50:00", "2026-02-03 09:30:00"]));

--seed--

--seed-contents--

js
function digitalDetox(logs) {

  return logs;
}

--solutions--

js
function digitalDetox(logs) {
  if (logs.length === 0) return true;

  const times = logs
    .map(log => new Date(log.replace(" ", "T") + "Z").getTime())
    .sort((a, b) => a - b);

  const ONE_DAY = 24 * 60 * 60 * 1000;
  const FOUR_HOURS = 4 * 60 * 60 * 1000;

  for (let i = 1; i < times.length; i++) {
    if (times[i] - times[i - 1] < FOUR_HOURS) {
      return false;
    }
  }

  const dailyCounts = {};
  for (const time of times) {
    const day = new Date(time).toISOString().slice(0, 10);
    dailyCounts[day] = (dailyCounts[day] || 0) + 1;
    if (dailyCounts[day] > 2) {
      return false;
    }
  }

  return true;
}