diff --git a/javascript/chronometer.js b/javascript/chronometer.js index 7a1349680..c5bb8d737 100644 --- a/javascript/chronometer.js +++ b/javascript/chronometer.js @@ -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}`; } } diff --git a/javascript/index.js b/javascript/index.js index fb3a43ab4..c0ff3afa8 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -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 @@ -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(); + } + });