diff --git a/javascript/chronometer.js b/javascript/chronometer.js index 7a13496..e40aaae 100644 --- a/javascript/chronometer.js +++ b/javascript/chronometer.js @@ -1,34 +1,48 @@ 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 (callback) 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 String(value).padStart(2, '0'); } 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()); + return `${minutes}:${seconds}`; } } diff --git a/javascript/index.js b/javascript/index.js index fb3a43a..c079d2b 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -15,51 +15,99 @@ 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 splitTime = chronometer.split(); + const li = document.createElement('li'); + li.className = 'list-item'; + li.innerHTML = splitTime; + splitsElement.appendChild(li); + } 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')) { + chronometer.start(printTime); + setStopBtn(); + setSplitBtn(); + } else { + chronometer.stop(); + setStartBtn(); + setResetBtn(); + } }); // Reset/Split Button btnRightElement.addEventListener('click', () => { // ... your code goes here + if (btnRightElement.classList.contains('reset')) { + chronometer.reset(); + clearSplits(); + printTime(); + } else { + printSplit(); + } });