Skip to content
Open

Solved #1974

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
22 changes: 22 additions & 0 deletions javascript/chronometer.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,56 @@
class Chronometer {
constructor() {
// ... your code goes here
this.currentTime = 0;
this.intervalId = null;
}

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

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

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

computeTwoDigitNumber(value) {
// ... your code goes here
return ('0' + value).slice(-2);
}

stop() {
// ... your code goes here
if (this.intervalId !== null) {
clearInterval(this.intervalId);
this.intervalId = null;
}
}

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

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

return `${formattedMinutes}:${formattedSeconds}`;
}
}

Expand Down
43 changes: 43 additions & 0 deletions javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ const splitsElement = document.getElementById('splits');

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

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
Expand All @@ -32,34 +40,69 @@ function printMilliseconds() {

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

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

function setStopBtn() {
// ... your code goes here
btnLeftElement.innerHTML = 'STOP';
btnLeftElement.classList.remove('start');
btnLeftElement.classList.add('stop');
}

function setSplitBtn() {
// ... your code goes here
btnRightElement.innerHTML = 'SPLIT';
btnRightElement.classList.remove('reset');
btnRightElement.classList.add('split');
}

function setStartBtn() {
// ... your code goes here
btnLeftElement.innerHTML = 'START';
btnLeftElement.classList.remove('stop');
btnLeftElement.classList.add('start');
}

function setResetBtn() {
// ... your code goes here
btnRightElement.innerHTML = 'RESET';
btnRightElement.classList.remove('split');
btnRightElement.classList.add('reset');
}

// Start/Stop Button
btnLeftElement.addEventListener('click', () => {
// ... your code goes here
if (btnLeftElement.classList.contains('start')) {
setStopBtn();
setSplitBtn();
chronometer.start(printTime);
} else {
setStartBtn();
setResetBtn();
chronometer.stop();
}
});

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

});