diff --git a/src/chronometer.js b/src/chronometer.js index 83c75bd2..4527e4ed 100644 --- a/src/chronometer.js +++ b/src/chronometer.js @@ -1,33 +1,60 @@ class Chronometer { constructor() { - // ... your code goes here + this.currentTime = 0; + this.intervalId = null; } start(printTimeCallback) { - // ... your code goes here + if (this.intervalId) return; // Prevent multiple intervals + this.intervalId = setInterval(() => { + this.currentTime++; + if (printTimeCallback) { + printTimeCallback(); + } + }, 1000); } getMinutes() { // ... your code goes here + return Math.floor(this.currentTime / 60); } getSeconds() { // ... your code goes here + return this.currentTime % 60; } + getMilliseconds() { + // ... your code goes here + return (this.currentTime % 1) * 1000; + } + computeTwoDigitNumber(value) { // ... your code goes here + return value < 10 ? `0${value}` : `${value}`; } stop() { // ... your code goes here + if (this.intervalId) { + clearInterval(this.intervalId); + this.intervalId = null; + } } reset() { // ... your code goes here + this.currentTime = 0; + if (this.intervalId) { + clearInterval(this.intervalId); + this.intervalId = null; + } } split() { // ... your code goes here + const minutes = this.computeTwoDigitNumber(this.getMinutes()); + const seconds = this.computeTwoDigitNumber(this.getSeconds()); + return `${minutes}:${seconds}`; } }