-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHumanReadableTime.js
More file actions
35 lines (33 loc) · 915 Bytes
/
Copy pathHumanReadableTime.js
File metadata and controls
35 lines (33 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 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.
*
* Kata URL: https://www.codewars.com/kata/52685f7382004e774f0001f7
*
*
* @param {*} seconds
* @returns
*/
function humanReadable(seconds) {
let HH = pad(Math.floor(seconds / 3600), 2);
seconds = seconds - HH * 3600;
let MM = pad(Math.floor(seconds / 60), 2);
seconds = seconds - MM * 60;
let SS = pad(seconds, 2);
function pad(num, size) {
num = num.toString();
while (num.length < size) num = "0" + num;
return num;
}
return `${HH}:${MM}:${SS}`;
}