-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhumanReadableDurationFormat.js
More file actions
195 lines (175 loc) · 5.72 KB
/
humanReadableDurationFormat.js
File metadata and controls
195 lines (175 loc) · 5.72 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way.
// The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes
// and seconds.
// It is much easier to understand with an example:
// formatDuration(62) // returns "1 minute and 2 seconds"
// formatDuration(3662) // returns "1 hour, 1 minute and 2 seconds"
// For the purpose of this Kata, a year is 365 days and a day is 24 hours.
// Note that spaces are important.
// Detailed rules
// The resulting expression is made of components like 4 seconds, 1 year, etc. In general, a positive integer and one of the valid units of time, separated by a space.
// The unit of time is used in plural if the integer is greater than 1.
// The components are separated by a comma and a space (", "). Except the last component, which is separated by " and ", just like it would be written in English.
// A more significant units of time will occur before than a least significant one. Therefore, 1 second and 1 year is not correct, but 1 year and 1 second is.
// Different components have different unit of times. So there is not repeated units like in 5 seconds and 1 second.
// A component will not appear at all if its value happens to be zero. Hence, 1 minute and 0 seconds is not valid, but it should be just 1 minute.
// A unit of time must be used "as much as possible". It means that the function should not return 61 seconds, but 1 minute and 1 second instead. Formally, the duration
// specified by of a component must not be greater than any valid more significant unit of time.
const formatDuration = (seconds) => {
if (seconds === 0) return "now";
let sec, min, hour, day, year, time = seconds, arr = [], obj = {};
if (time >= 60) {
sec = time % 60;
obj["sec"] = sec;
min = Math.floor(time/60);
obj["min"] = min;
time = min;
} else {
obj["sec"] = time;
}
if (time >= 60) {
min = time % 60;
obj["min"] = min;
hour = Math.floor(time/60);
obj["hour"] = hour;
time = hour;
}
if (time >= 24) {
hour = time % 24;
obj["hour"] = hour;
day = Math.floor(time/24);
obj["day"] = day;
time = day;
}
if (time >= 365) {
day = time % 365;
obj["day"] = day;
year = Math.floor(time/365);
obj["year"] = year;
}
if (obj["sec"]) {
if (obj["min"] || obj["hour"] || obj["day"] || obj["year"]) {
if (obj["sec"] === 1) {
arr.unshift(" and 1 second")
} else {
arr.unshift(" and " + sec.toString() + " seconds");
}
} else {
if (obj["sec"] === 1){
return "1 second";
} else {
return sec.toString() + " seconds";
}
}
}
if (obj["min"]) {
if (obj["hour"] || obj["day"] || obj["year"]) {
if (arr.length < 1) {
if (obj["min"] === 1) {
arr.unshift(" and 1 minute");
} else {
arr.unshift(" and " + min.toString() + " minutes");
}
} else {
if (obj["min"] === 1) {
arr.unshift(", 1 minute");
} else {
arr.unshift(", " + min.toString() + " minutes")
}
}
} else {
if (obj["min"] === 1) {
arr.unshift("1 minute");
} else {
arr.unshift(min.toString() + " minutes");
}
}
}
if (obj["hour"]) {
if (obj["day"] || obj["year"]) {
if (arr.length < 1) {
if (obj["hour"] === 1) {
arr.unshift(" and 1 hour");
} else {
arr.unshift(" and " + hour.toString() + " hours");
}
} else {
if (obj["hour"] === 1) {
arr.unshift(", 1 hour");
} else {
arr.unshift(", " + hour.toString() + " hours");
}
}
} else {
if (obj["hour"] === 1) {
arr.unshift("1 hour");
} else {
arr.unshift(hour.toString() + " hours");
}
}
}
if (obj["day"]) {
if (obj["year"]) {
if (arr.length < 1) {
if (obj["day"] === 1) {
arr.unshift(" and 1 day");
} else {
arr.unshift(" and " + day.toString() + " days");
}
} else {
if (obj["day"] === 1) {
arr.unshift(", 1 day");
} else {
arr.unshift(", " + day.toString() + " days");
}
}
} else {
if (obj["day"] === 1) {
arr.unshift("1 day");
} else {
arr.unshift(day.toString() + " days");
}
}
}
if (obj["year"]) {
if (arr.length < 1) {
if (obj["year"] === 1) {
return "1 year";
} else {
return year.toString() + " years";
}
} else {
if (obj["year"] === 1) {
arr.unshift("1 year");
} else {
arr.unshift(year.toString() + " years");
}
}
}
return arr.join("");
}
const convert = () => {
const value = document.querySelector('input').value,
appendDiv = document.createElement('div'),
div = document.querySelector('#appendToMe'),
output = formatDuration(value);
if (value.length > 0) {
appendDiv.append(value + " seconds equals to " + output);
appendDiv.classList.add('output')
div.append(appendDiv);
}
}
const clear = () => {
let input = document.querySelector('#input');
input.value = "";
}
const input = document.querySelector('#input'),
convertButton = document.querySelector('#convert'),
clearButton = document.querySelector('#clear'),
resetAll = document.querySelector('#reset');
input.addEventListener('keypress', (event) => {
if (event.keyCode === 13) convert(); });
convertButton.addEventListener('click', convert);
clearButton.addEventListener('click', clear);
resetAll.addEventListener('click',
window.location.reload.bind(window.location));