Human Readable Time

SungJunEun·2021년 11월 8일
0

Codewars 문제풀이

목록 보기
11/26
post-thumbnail

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.

My solution:

function humanReadable (seconds) {
  // get number
  const hour = parseInt(seconds / 3600);
  const minute = parseInt(seconds % 3600 / 60);
  const second = parseInt(seconds % 3600 % 60);
  
  // modify number into given form
  function modify(number) {
    let stringNumber = String(number);
    if(stringNumber.length == 1) {
      stringNumber = '0'+stringNumber;
    }
    return stringNumber;
  }
  
  return modify(hour)+':'+modify(minute)+':'+modify(second);
}

Best solutions:

별반 다르지 않다.

profile
블록체인 개발자(진)

0개의 댓글