Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions src/chronometer-centiseconds.js
Original file line number Diff line number Diff line change
@@ -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;
31 changes: 23 additions & 8 deletions src/chronometer.js
Original file line number Diff line number Diff line change
@@ -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;