From 56ec2fdf430a2c3c44380c9d75c0e5294ee7946e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Baldino?= Date: Thu, 10 Apr 2025 17:42:11 +0100 Subject: [PATCH] first commit --- src/chronometer.js | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/chronometer.js b/src/chronometer.js index 83c75bd2..3e25145c 100644 --- a/src/chronometer.js +++ b/src/chronometer.js @@ -1,33 +1,57 @@ class Chronometer { constructor() { - // ... your code goes here + this.currentTime = 0, + this.intervalId = null } start(printTimeCallback) { - // ... your code goes here + this.intervalId = setInterval(() => { + this.currentTime++; + if (printTimeCallback) { + printTimeCallback(this.currentTime); + } + }, 1000); } getMinutes() { - // ... your code goes here + let minutes = 0 + + minutes = this.currentTime / 60 + + return Math.floor(minutes) } getSeconds() { - // ... your code goes here + let seconds = 0 + + seconds = this.currentTime % 60 + + return seconds } computeTwoDigitNumber(value) { - // ... your code goes here + if (value < 10) { + return `0${value}` + } + if (value >= 10) { + return `${value}` + } } stop() { - // ... your code goes here + clearInterval(this.intervalId) } reset() { - // ... your code goes here + this.currentTime = 0 } split() { - // ... your code goes here + + const minutes = this.getMinutes() + const seconds = this.getSeconds() + + + return `${this.computeTwoDigitNumber(minutes)}:${this.computeTwoDigitNumber(seconds)}` } }