Skip to content

Chronometer Implementation Solved #1948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
41 changes: 33 additions & 8 deletions javascript/chronometer.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,59 @@
class Chronometer {
constructor() {
// ... your code goes here
this.currentTime = 0;
this.intervalId = null;
this.milliseconds = 0;
this.millisecondsIntervalId = null;
}

start(callback) {
// ... your code goes here
this.intervalId = setInterval(() => {
this.currentTime += 1;
if (callback) {
callback();
}
}, 1000);

// For milliseconds (bonus)
this.millisecondsIntervalId = setInterval(() => {
this.milliseconds = (this.milliseconds + 1) % 100;
}, 10);
}

getMinutes() {
// ... your code goes here
return Math.floor(this.currentTime / 60);
}

getSeconds() {
// ... your code goes here
return this.currentTime % 60;
}

getMilliseconds() {
return this.milliseconds;
}

computeTwoDigitNumber(value) {
// ... your code goes here
return value < 10 ? `0${value}` : `${value}`;
}

stop() {
// ... your code goes here
clearInterval(this.intervalId);
clearInterval(this.millisecondsIntervalId);
}

reset() {
// ... your code goes here
this.currentTime = 0;
this.milliseconds = 0;
}

split() {
// ... your code goes here
const minutes = this.computeTwoDigitNumber(this.getMinutes());
const seconds = this.computeTwoDigitNumber(this.getSeconds());
const milliseconds = this.computeTwoDigitNumber(this.getMilliseconds());

// Uncomment the line below and comment out the next line to enable milliseconds in split
// return `${minutes}:${seconds}:${milliseconds}`;
return `${minutes}:${seconds}`;
}
}

Expand Down
67 changes: 55 additions & 12 deletions javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,95 @@ const milUniElement = document.getElementById('milUni');
const splitsElement = document.getElementById('splits');

function printTime() {
// ... your code goes here
printMinutes();
printSeconds();
printMilliseconds();
}

function printMinutes() {
// ... your code goes here
const minutes = chronometer.computeTwoDigitNumber(chronometer.getMinutes());
minDecElement.innerHTML = minutes[0];
minUniElement.innerHTML = minutes[1];
}

function printSeconds() {
// ... your code goes here
const seconds = chronometer.computeTwoDigitNumber(chronometer.getSeconds());
secDecElement.innerHTML = seconds[0];
secUniElement.innerHTML = seconds[1];
}

// ==> BONUS
function printMilliseconds() {
// ... your code goes here
const milliseconds = chronometer.computeTwoDigitNumber(chronometer.getMilliseconds());
milDecElement.innerHTML = milliseconds[0];
milUniElement.innerHTML = milliseconds[1];
}

function printSplit() {
// ... your code goes here
const li = document.createElement('li');
li.className = 'list-item';
li.innerHTML = chronometer.split();
splitsElement.appendChild(li);
}

function clearSplits() {
// ... your code goes here
splitsElement.innerHTML = '';
}

function setStopBtn() {
// ... your code goes here
btnLeftElement.className = 'btn stop';
btnLeftElement.innerHTML = 'STOP';
}

function setSplitBtn() {
// ... your code goes here
btnRightElement.className = 'btn split';
btnRightElement.innerHTML = 'SPLIT';
}

function setStartBtn() {
// ... your code goes here
btnLeftElement.className = 'btn start';
btnLeftElement.innerHTML = 'START';
}

function setResetBtn() {
// ... your code goes here
btnRightElement.className = 'btn reset';
btnRightElement.innerHTML = 'RESET';
}

// Start/Stop Button
btnLeftElement.addEventListener('click', () => {
// ... your code goes here
if (btnLeftElement.classList.contains('start')) {
// Change button status to "running"
setStopBtn();
setSplitBtn();

// Start the chronometer with a callback to update the display
chronometer.start(() => {
printTime();
});
} else {
// Change button status to "stopped"
setStartBtn();
setResetBtn();

// Stop the chronometer
chronometer.stop();
}
});

// Reset/Split Button
btnRightElement.addEventListener('click', () => {
// ... your code goes here
if (btnRightElement.classList.contains('reset')) {
// Reset the chronometer
chronometer.reset();

// Update the display
printTime();

// Clear the splits list
clearSplits();
} else {
// Record a split time
printSplit();
}
});
6 changes: 3 additions & 3 deletions styles/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ body {
}

.leash {
background: rgb(204, 84, 87);
border: 2px solid rgb(134, 59, 61);
background: #000000;
border: 2px solid #333333;
height: 50px;
margin: 0 auto;
width: 120px;
Expand All @@ -79,7 +79,7 @@ body {

.hole {
background: #fff;
border: 2px solid rgb(134, 59, 61);
border: 2px solid #333333;
border-radius: 100%;
height: 20px;
margin: 0 auto 50px;
Expand Down