Back to Freecodecamp

Challenge 184: 2026 Winter Games Day 5: Cross-Country Skiing

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/697a49e6ff50d756c9b69361.md

latest2.8 KB
Original Source

--description--

Given an array of finish times for a cross-country ski race, convert them into times behind the winner.

  • Given times are strings in "H:MM:SS" format.
  • Given times will be in order from fastest to slowest.
  • The winners time (fastest time) should correspond to "0".
  • Each other time should show the time behind the winner, in the format "+M:SS".

For example, given ["1:25:32", "1:26:10", "1:27:05"], return ["0", "+0:38", "+1:33"].

--hints--

getRelativeResults(["1:25:32", "1:26:10", "1:27:05"]) should return ["0", "+0:38", "+1:33"].

js
assert.deepEqual(getRelativeResults(["1:25:32", "1:26:10", "1:27:05"]), ["0", "+0:38", "+1:33"]);

getRelativeResults(["1:00:01", "1:00:05", "1:00:10"]) should return ["0", "+0:04", "+0:09"].

js
assert.deepEqual(getRelativeResults(["1:00:01", "1:00:05", "1:00:10"]), ["0", "+0:04", "+0:09"]);

getRelativeResults(["1:10:06", "1:10:23", "1:10:48", "1:12:11"]) should return ["0", "+0:17", "+0:42", "+2:05"].

js
assert.deepEqual(getRelativeResults(["1:10:06", "1:10:23", "1:10:48", "1:12:11"]), ["0", "+0:17", "+0:42", "+2:05"]);

getRelativeResults(["0:49:13", "0:49:15", "0:50:14", "0:51:30", "0:51:58", "0:52:16", "0:53:12", "0:53:31", "0:56:19", "1:02:20"]) should return ["0", "+0:02", "+1:01", "+2:17", "+2:45", "+3:03", "+3:59", "+4:18", "+7:06", "+13:07"].

js
assert.deepEqual(getRelativeResults(["0:49:13", "0:49:15", "0:50:14", "0:51:30", "0:51:58", "0:52:16", "0:53:12", "0:53:31", "0:56:19", "1:02:20"]), ["0", "+0:02", "+1:01", "+2:17", "+2:45", "+3:03", "+3:59", "+4:18", "+7:06", "+13:07"]);

getRelativeResults(["2:01:15", "2:10:45", "2:10:53", "2:11:04", "2:11:55", "2:13:27", "2:14:30", "2:15:10"]) should return ["0", "+9:30", "+9:38", "+9:49", "+10:40", "+12:12", "+13:15", "+13:55"].

js
assert.deepEqual(getRelativeResults(["2:01:15", "2:10:45", "2:10:53", "2:11:04", "2:11:55", "2:13:27", "2:14:30", "2:15:10"]), ["0", "+9:30", "+9:38", "+9:49", "+10:40", "+12:12", "+13:15", "+13:55"]);

--seed--

--seed-contents--

js
function getRelativeResults(results) {

  return results;
}

--solutions--

js
function getRelativeResults(results) {
  const timeToSeconds = (timeStr) => {
    const [hours, minutes, seconds] = timeStr.split(':').map(Number);
    return hours * 3600 + minutes * 60 + seconds;
  };

  const secondsToTimeFormat = (secs) => {
    const mins = Math.floor(secs / 60);
    const secsRemainder = secs % 60;
    return `+${mins}:${String(secsRemainder).padStart(2, '0')}`;
  };

  const winnerSeconds = timeToSeconds(results[0]);

  return results.map((time, index) => {
    if (index === 0) return '0';
    const currentSeconds = timeToSeconds(time);
    const difference = currentSeconds - winnerSeconds;
    return secondsToTimeFormat(difference);
  });
}