Journal Archive

function humanReadable (seconds) {
  // handle error input
  if (seconds > 359999) return "Sorry, too many seconds for HH:MM:SS format";
  // workout number of hours, minutes and seconds
  let HH = Math.floor(seconds / 3600);
  let MM = Math.floor(seconds / 60) % 60;
  let SS = seconds % 60;
  
  // reformat to add leading zero for numbers less than 10
  HH = HH < 10 ? "0" + HH: HH;
  MM = MM < 10 ? "0" + MM: MM;
  SS = SS < 10 ? "0" + SS: SS;
  
  return HH + ":" + MM + ":" + SS;
}

Day 5: Solving one of the Kata on CodeWars

Human Readable Time 5 kyu

Write a function, which takes a non-negative integer (seconds) as input and returns the time in a human-readable format (HH:MM:SS).

HH = hours, padded to 2 digits, range: 00 - 99 MM = minutes, padded to 2 digits, range: 00 - 59 SS = seconds, padded to 2 digits, range: 00 - 59

The maximum time never exceeds 359999 (99:59:59)