-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime_Log.js
More file actions
74 lines (58 loc) · 1.92 KB
/
Time_Log.js
File metadata and controls
74 lines (58 loc) · 1.92 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
/* ------------------------------------------------------------------------
You will be given an array of events, which are represented by
strings. The strings are dates in HH:MM:SS format.
It is known that all events are recorded in chronological order
and two events can't occur in the same second. Return the minimum
number of days during which the log is written.
Example:
Input -> ["12:12:12"]
Output -> 1
Input -> ["12:00:00", "23:59:59", "00:00:00"]
Output -> 2
Input -> []
Output -> 0
------------------------------------------------------------------------ */
/*
Date.prototype.setUTCHours(): changes the hours, minutes, seconds, and/or milliseconds
for this date according to universal time
Array.prototype.slice(): returns a shallow copy of a portion of an array into a new array
object selected from start to end
Date.prototype.getTime(): returns the number of milliseconds for this date since the epoch,
which is defined as the midnight at the beginning of January 1,
1970, UTC
*/
function checkLogs(log) {
let days = 1;
let actual = new Date(2020, 1, 1, 0, 0, 0);
let next = new Date(2020, 1, 1, 0, 0, 0);
if (log.length <= 1) {
return log.length;
} else {
for (let i = 0; i < log.length - 1; i++) {
actual.setUTCHours(
log[i].slice(0, 2),
log[i].slice(3, 5),
log[i].slice(6, 8)
);
next.setUTCHours(
log[i + 1].slice(0, 2),
log[i + 1].slice(3, 5),
log[i + 1].slice(6, 8)
);
if (actual.getTime() >= next.getTime()) days++;
}
return days;
}
}
/*
Alternative solution
*/
function checkLogs(log) {
let prev = "99:99:99";
let days = 0;
log.forEach((t) => {
if (t <= prev) days++;
prev = t;
});
return days;
}