-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhuman_readable_time.js
More file actions
31 lines (27 loc) · 867 Bytes
/
human_readable_time.js
File metadata and controls
31 lines (27 loc) · 867 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
/**
* I have no idea why this is 5 kyu...
*
* @author Bradley Whited
* @see https://www.codewars.com/kata/human-readable-time/javascript
* @rank 5 kyu
*/
function humanReadable(seconds) {
return "" + formatNum(Math.floor(seconds / 60 / 60)) +
":" + formatNum(Math.floor(seconds / 60) % 60) +
":" + formatNum(seconds % 60);
}
function formatNum(num) {
return ((num < 10) ? '0' : '') + num;
}
console.log(humanReadable(0)); // '00:00:00'
console.log(humanReadable(5)); // '00:00:05'
console.log(humanReadable(60)); // '00:01:00'
console.log(humanReadable(86399)); // '23:59:59'
console.log(humanReadable(359999)); // '99:59:59'
if(process.argv.length > 2) {
console.log();
for(var i = 2; i < process.argv.length; ++i) {
var secs = process.argv[i];
console.log(secs + ' => ' + humanReadable(secs));
}
}