Coding Challenge - #6: Format Elapsed Time

Write a function formatDuration(seconds) that converts a non-negative integer of seconds into a "MM:SS" or "HH:MM:SS" string. Omit the hours portion if the duration is under one hour, and ensure all parts are zero-padded to two digits (for example, 65 becomes "01:05" and 3661 becomes "01:01:01").

function formatDuration(seconds) {
  // Your code here
}

Rules:

  • Assume seconds is always an integer >= 0
  • Single-digit values must be zero-padded (e.g. 5 seconds → “00:05”)
  • Return “HH:MM:SS” only when seconds >= 3600, otherwise return “MM:SS”

Post your solution as a reply. Answer goes up in about a day.