-
Notifications
You must be signed in to change notification settings - Fork 0
/
seconds_time_formating.js
52 lines (45 loc) · 1.17 KB
/
seconds_time_formating.js
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
45
46
47
48
49
50
51
52
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
const INTERVAL_TO_SECONDS = {
week: 604800,
day: 86400,
hour: 3600,
minute: 60,
second: 1,
};
const INTERVAL_TO_SECONDS_FORMAT = {
week: "w",
day: "d",
hour: "h",
minute: "m",
second: "s",
};
function solution(X) {
// write your code in JavaScript (Node.js 8.9.4)
let remainder = X;
let maxTransformation = 2;
let numberOfTransformation = 0;
let answers = [];
for (const [interval, valueInSeconds] of Object.entries(
INTERVAL_TO_SECONDS
)) {
if (remainder / valueInSeconds > 1) {
if (numberOfTransformation === maxTransformation) {
answers[maxTransformation - 1] = [
answers[maxTransformation - 1][0] + 1,
answers[maxTransformation - 1][1],
];
break;
} else {
answers.push([
Math.floor(remainder / valueInSeconds),
INTERVAL_TO_SECONDS_FORMAT[interval],
]);
}
remainder = remainder % valueInSeconds;
numberOfTransformation++;
}
}
const answer = answers.map(([value, format]) => `${value}${format}`).join("");
return answer;
}