-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhuman_readable_time.js
More file actions
44 lines (40 loc) · 2.69 KB
/
human_readable_time.js
File metadata and controls
44 lines (40 loc) · 2.69 KB
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
36
37
38
39
40
41
42
43
44
/******************************************************************************************
* CODEWARS HUMAN READABLE TIME CHALLENGE *
* *
* Problem Statement *
* Write a function, which takes a non-negative integer (seconds) as input & 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) *
* *
* Examples *
* Input 1: 0 *
* Output 1: 00:00:00 *
* *
* Input 2: 60 *
* Output 2: 00:01:00 *
* *
* Input 3: 86399 *
* Output 3: 23:59:59 *
*****************************************************************************************/
function formatTimeStamp(hours, minutes, second) {
let hoursTimeStamp;
let minutesTimeStamp;
let secondsTimeStamp;
hoursTimeStamp = hours > 9 ? "" + hours : "0" + hours;
minutesTimeStamp = minutes > 9 ? "" + minutes : "0" + minutes;
secondsTimeStamp = second > 9 ? "" + second : "0" + second;
return [hoursTimeStamp, minutesTimeStamp, secondsTimeStamp];
}
function humanReadable(seconds) {
let hours = Math.floor(seconds / 3600);
seconds %= 3600;
let minutes = Math.floor(seconds / 60);
let second = seconds % 60;
const [HH, MM, SS] = formatTimeStamp(hours, minutes, second);
return HH + ":" + MM + ":" + SS;
}