mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-16 07:00:53 -04:00
Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com> Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
1.4 KiB
1.4 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 68216eb60f957572e7c340c4 | JavaScript Challenge 11: Mile Pace | 28 | javascript-challenge-11 |
--description--
Given a number of miles ran, and a time in "MM:SS" (minutes:seconds) it took to run those miles, return a string for the average time it took to run each mile in the format "MM:SS".
- Add leading zeros when needed.
--hints--
milePace(3, "24:00") should return "08:00".
assert.equal(milePace(3, "24:00"), "08:00");
milePace(1, "06:45") should return "06:45".
assert.equal(milePace(1, "06:45"), "06:45");
milePace(2, "07:00") should return "03:30".
assert.equal(milePace(2, "07:00"), "03:30");
milePace(26.2, "120:35") should return "04:36".
assert.equal(milePace(26.2, "120:35"), "04:36");
--seed--
--seed-contents--
function milePace(miles, duration) {
return miles;
}
--solutions--
function milePace(miles, duration) {
const [minutes, seconds] = duration.split(':').map(Number);
const totalSeconds = minutes * 60 + seconds;
const avgSecondsPerMile = totalSeconds / miles;
const avgMinutes = Math.floor(avgSecondsPerMile / 60);
const avgSeconds = Math.round(avgSecondsPerMile % 60);
const paddedMinutes = avgMinutes.toString().padStart(2, '0');
const paddedSeconds = avgSeconds.toString().padStart(2, '0');
return `${paddedMinutes}:${paddedSeconds}`;
}