Problem
- 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:MM:SS)으로 시간을 반환한다.
- The maximum time never exceeds 359999(99:59:59).
- 최대 시간은 359999(99:59:59)를 초과하지 않는다.
Solution 01
function humanReadable(seconds) {
let hour = parseInt(seconds / 3600);
let min = parseInt(seconds / 60) % 60;
let sec = seconds % 60;
return (
(parseInt(hour / 10) > 0 ? hour : '0' + hour) + ':' +
(parseInt(min / 10) > 0 ? min : '0' + min) + ':' +
(parseInt(sec / 10) > 0 ? sec : '0' + sec);
)
}
humanReadable(0); // 00:00:00
humanReadable(1); // 00:00:01
humanReadable(10); // 00:00:10
humanReadable(60); // 00:01:00
humanReadable(86399); // 23:59:59
humanReadable(359999); // 99:59:59
Solution 02
function humanReadable(seconds) {
let pad = val => val > 10 ? val : '0' + val;
return pad(parseInt(seconds / 3600)) + ':' + pad(parseInt((seconds / 60) % 60)) + ':' + pad(seconds % 60);
}
humanReadable(0); // 00:00:00
humanReadable(1); // 00:00:01
humanReadable(10); // 00:00:10
humanReadable(60); // 00:01:00
humanReadable(86399); // 23:59:59
humanReadable(359999); // 99:59:59
Solution 03
function humanReadable(seconds) {
let pad = val => val > 10 ? val : '0' + val;
let hour = parseInt(seconds / 3600);
let min = parseInt((seconds / 60) % 60);
let sec = seconds % 60;
return pad(hour) + ':' + pad(min) + ':' + pad(sec);
}
humanReadable(0); // 00:00:00
humanReadable(1); // 00:00:01
humanReadable(10); // 00:00:10
humanReadable(60); // 00:01:00
humanReadable(86399); // 23:59:59
humanReadable(359999); // 99:59:59