From fa4f1f951c3b3a4691e408402af156a0bbfd6668 Mon Sep 17 00:00:00 2001 From: Akriti sharma Date: Sun, 23 Nov 2025 18:57:40 +0100 Subject: [PATCH] Solved Chronometer Lab --- src/chronometer-centiseconds.js | 38 +++++++++++++++++++++++++-------- src/chronometer.js | 31 ++++++++++++++++++++------- 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/chronometer-centiseconds.js b/src/chronometer-centiseconds.js index 8337b5b6..559e6577 100644 --- a/src/chronometer-centiseconds.js +++ b/src/chronometer-centiseconds.js @@ -1,37 +1,57 @@ +/ src/chronometer-centiseconds.js + class Chronometer { constructor() { - // ... your code goes here + this.currentTime = 0; // centiseconds elapsed + this.intervalId = null; } + // Start the timer, counting centiseconds start(printTimeCallback) { - // ... your code goes here + this.intervalId = setInterval(() => { + this.currentTime++; // increment by 1 centisecond (10ms) + if (printTimeCallback) printTimeCallback(); + }, 10); // 10ms = 1 centisecond } + // Return full minutes getMinutes() { - // ... your code goes here + return Math.floor(this.currentTime / 6000); // 6000 centiseconds = 1 minute } + // Return seconds in the current minute getSeconds() { - // ... your code goes here + return Math.floor((this.currentTime % 6000) / 100); // remaining full seconds } + // Return centiseconds in the current second getCentiseconds() { - // ... your code goes here + return this.currentTime % 100; } + // Format any number as two digits (e.g., 7 => "07") computeTwoDigitNumber(value) { - // ... your code goes here + return value < 10 ? `0${value}` : `${value}`; } + // Stop the timer stop() { - // ... your code goes here + clearInterval(this.intervalId); } + // Reset the timer to 0 reset() { - // ... your code goes here + this.currentTime = 0; } + // Return formatted time "mm:ss:cs" split() { - // ... your code goes here + const minutes = this.computeTwoDigitNumber(this.getMinutes()); + const seconds = this.computeTwoDigitNumber(this.getSeconds()); + const centiseconds = this.computeTwoDigitNumber(this.getCentiseconds()); + return `${minutes}:${seconds}:${centiseconds}`; } } + +// If using modules, uncomment the following line +// module.exports = Chronometer; diff --git a/src/chronometer.js b/src/chronometer.js index 83c75bd2..cb085e8d 100644 --- a/src/chronometer.js +++ b/src/chronometer.js @@ -1,33 +1,48 @@ class Chronometer { constructor() { - // ... your code goes here + this.currentTime = 0; // centiseconds elapsed + this.intervalId = null; } + // Start the timer, counting centiseconds start(printTimeCallback) { - // ... your code goes here + this.intervalId = setInterval(() => { + this.currentTime++; // increment by 1 centisecond (10ms) + if (printTimeCallback) printTimeCallback(); + }, 10); // 10ms = 1 centisecond } getMinutes() { - // ... your code goes here + return Math.floor(this.currentTime / 6000); // 6000 centiseconds = 1 minute } getSeconds() { - // ... your code goes here + return Math.floor((this.currentTime % 6000) / 100); // remaining full seconds + } + + getCentiseconds() { + return this.currentTime % 100; // remaining centiseconds in current second } computeTwoDigitNumber(value) { - // ... your code goes here + return value < 10 ? `0${value}` : `${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.computeTwoDigitNumber(this.getMinutes()); + const seconds = this.computeTwoDigitNumber(this.getSeconds()); + const centiseconds = this.computeTwoDigitNumber(this.getCentiseconds()); + return `${minutes}:${seconds}:${centiseconds}`; } } + +// If using modules, uncomment this line: +// module.exports = Chronometer;