Brainlag
  1. Library
  2. Challenges
  3. Web

Playlist length

Turn seconds into the 3:05 and 1:02:05 times music apps show, then add up a whole playlist from its track lengths.

WebCore25 min

Write two functions.

formatDuration(seconds) returns the time the way a music app shows it: minutes and two-digit seconds (185 gives "3:05"), and once there is at least an hour, hours too, with two-digit minutes (3725 gives "1:02:05").

playlistLength(tracks) takes tracks like { title: "Neon Rain", length: "3:25" } and returns the total, formatted the same way.

JavaScript
playlistLength([{ title: "Neon Rain", length: "3:25" }, { title: "Late Bus", length: "4:40" }]);
// "8:05"

Checks

  1. Minutes and seconds
  2. Hours
  3. Add up a playlist
exercise_1.js to run
function formatDuration(seconds) {
}

function playlistLength(tracks) {
}

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.