diff --git a/javascript/chronometer.js b/javascript/chronometer.js index 7a1349680..83237fbb3 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 (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 + let stringValue = String(value); + let paddedString = '00' + stringValue; + return paddedString.slice(-2); } stop() { - // ... your code goes here + clearInterval(this.intervalId); } 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}`; } } diff --git a/javascript/index.js b/javascript/index.js index fb3a43ab4..c5d9c77b6 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -14,15 +14,22 @@ const milUniElement = document.getElementById('milUni'); const splitsElement = document.getElementById('splits'); function printTime() { - // ... your code goes here + printMinutes(); + printSeconds(); } function printMinutes() { - // ... your code goes here + const minutes = chronometer.getMinutes(); + const formattedMinutes = chronometer.computeTwoDigitNumber(minutes); + minDecElement.innerHTML = formattedMinutes[0]; + minUniElement.innerHTML = formattedMinutes[1]; } function printSeconds() { - // ... your code goes here + const seconds = chronometer.getSeconds(); + const formattedSeconds = chronometer.computeTwoDigitNumber(seconds); + secDecElement.innerHTML = formattedSeconds[0]; + secUniElement.innerHTML = formattedSeconds[1]; } // ==> BONUS @@ -31,35 +38,58 @@ function printMilliseconds() { } function printSplit() { - // ... your code goes here + const splitTime = chronometer.split(); + const li = document.createElement('li'); + li.innerHTML = splitTime; + splitsElement.appendChild(li); } function clearSplits() { - // ... your code goes here + splitsElement.innerHTML = ''; } function setStopBtn() { - // ... your code goes here + btnLeftElement.innerHTML = 'STOP'; + btnLeftElement.className = 'btn stop'; } function setSplitBtn() { - // ... your code goes here + btnRightElement.innerHTML = 'SPLIT'; + btnRightElement.className = 'btn split'; } function setStartBtn() { - // ... your code goes here + btnLeftElement.innerHTML = 'START'; + btnLeftElement.className = 'btn start'; } function setResetBtn() { - // ... your code goes here + btnRightElement.innerHTML = 'RESET'; + btnRightElement.className = 'btn 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(); + printMinutes(); + printSeconds(); + clearSplits(); + } else { + chronometer.split(); + printSplit(); + } });