본문 바로가기
알고리즘/Codewars

[Codewars] Human Readable Time (5 kyu) / JavaScript

by fluss 2023. 1. 15.

https://www.codewars.com/kata/52685f7382004e774f0001f7

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

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)

You can find some examples in the test fixtures.

 

코드

function humanReadable (seconds) {
  const h = parseInt(seconds / 3600);
  const m = parseInt(seconds % 3600 / 60);
  const s = (seconds % 60);
  return fm(h) + ':' + fm(m) + ':' + fm(s);
}

function fm(num){
  return num.toString().padStart(2, '0');
}

댓글