-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathchronometer.js
More file actions
57 lines (51 loc) · 1.3 KB
/
chronometer.js
File metadata and controls
57 lines (51 loc) · 1.3 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
class Chronometer {
constructor() {
// ... your code goes here
this.currentTime = 0;
this.intervalId = null;
}
start(callback) {
// ... your code goes here
this.intervalId = setInterval(() => {
this.currentTime++;
if (callback)
callback();
}, 1000)
}
getMinutes() {
// ... your code goes here
return Math.floor(this.currentTime / 60);
}
getSeconds() {
// ... your code goes here
return this.currentTime % 60;
}
computeTwoDigitNumber(value) {
// ... your code goes here
let formattedNumber = (value < 10) ? '0' + value.toString() : value.toString();
return formattedNumber;
}
stop() {
// ... your code goes here
if (this.intervalId !== null) {
clearInterval(this.intervalId);
this.intervalId = null;
}
clearInterval(this.intervalId)
}
reset() {
// ... your code goes here
this.currentTime = 0;
}
split() {
// ... your code goes here
const minute = this.computeTwoDigitNumber(this.getMinutes());
const second = this.computeTwoDigitNumber(this.getSeconds());
return `${minute}:${second}`;
}
}
// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */
if (typeof module !== 'undefined') {
module.exports = Chronometer;
}