From 2d27695e5fef7613ecf79779fb99f3cd951c7546 Mon Sep 17 00:00:00 2001 From: Yerko Arce Date: Sun, 26 Jan 2025 21:41:33 -0300 Subject: [PATCH] done with miliseconds --- javascript/chronometer.js | 47 ++++++++++++++++++++++------- javascript/index.js | 63 ++++++++++++++++++++++++++++----------- 2 files changed, 83 insertions(+), 27 deletions(-) diff --git a/javascript/chronometer.js b/javascript/chronometer.js index 7a13496..9255f0c 100644 --- a/javascript/chronometer.js +++ b/javascript/chronometer.js @@ -1,34 +1,61 @@ class Chronometer { constructor() { - // ... your code goes here + this.currentTime = 0 + this.intervalId = null } - start(callback) { - // ... your code goes here + start(callback) { + if (callback) { + this.intervalId = setInterval(() => { + this.currentTime += 10 + callback( + this.computeTwoDigitNumber(this.getMinutes()), + this.computeTwoDigitNumber(this.getSeconds()), + this.computeTwoDigitNumber(this.getMiliseconds()) + ) + }, 10) + } else { + this.intervalId = setInterval(() => { + this.currentTime++ + }, 10) + } + + } getMinutes() { - // ... your code goes here + return Math.floor(this.currentTime / 60000) } getSeconds() { - // ... your code goes here + return (Math.floor(this.currentTime / 1000)) % 60 + } + + getMiliseconds() { + return this.currentTime % 1000 } computeTwoDigitNumber(value) { - // ... your code goes here + if (value < 10) { + return '0' + value + } else if (value > 99) { + return Math.floor(value / 10).toString() + } + return value.toString() } stop() { - // ... your code goes here + clearInterval(this.intervalId) } - reset() { - // ... your code goes here + reset(callback) { + callback() + this.intervalId = null + this.currentTime = 0 } split() { - // ... your code goes here + return this.computeTwoDigitNumber(this.getMinutes()) + ':' + this.computeTwoDigitNumber(this.getSeconds()) + ':' + this.computeTwoDigitNumber(this.getMiliseconds()) } } diff --git a/javascript/index.js b/javascript/index.js index fb3a43a..9327a34 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -13,53 +13,82 @@ const milDecElement = document.getElementById('milDec'); const milUniElement = document.getElementById('milUni'); const splitsElement = document.getElementById('splits'); -function printTime() { - // ... your code goes here +function printTime(min, sec, mili) { + printSeconds(sec[0], sec[1]) + printMinutes(min[0], min[1]) + printMilliseconds(mili[0], mili[1]) } -function printMinutes() { - // ... your code goes here +function printMinutes(dec, uni) { + minDecElement.innerText = dec + minUniElement.innerText = uni } -function printSeconds() { - // ... your code goes here +function printSeconds(dec, uni) { + secDecElement.innerText = dec + secUniElement.innerText = uni } // ==> BONUS -function printMilliseconds() { - // ... your code goes here +function printMilliseconds(dec, uni) { + milDecElement.innerText = dec + milUniElement.innerText = uni } function printSplit() { - // ... your code goes here + const li = document.createElement('li') + li.textContent = chronometer.split() + splitsElement.appendChild(li) } function clearSplits() { - // ... your code goes here + splitsElement.innerHTML = '' + minDecElement.innerHTML = 0 + minUniElement.innerHTML = 0 + secDecElement.innerHTML = 0 + secUniElement.innerHTML = 0 + milDecElement.innerHTML = 0 + milUniElement.innerHTML = 0 } function setStopBtn() { - // ... your code goes here + btnLeftElement.classList = 'btn stop' + btnLeftElement.innerText = 'STOP' } function setSplitBtn() { - // ... your code goes here + btnRightElement.classList = 'btn split' + btnRightElement.innerText = 'SPLIT' } function setStartBtn() { - // ... your code goes here + btnLeftElement.classList = 'btn start' + btnLeftElement.innerText = 'START' } function setResetBtn() { - // ... your code goes here + btnRightElement.classList = 'btn reset' + btnRightElement.innerText = 'RESET' } // Start/Stop Button btnLeftElement.addEventListener('click', () => { - // ... your code goes here + if (btnLeftElement.classList.value === 'btn start'){ + setStopBtn() + setSplitBtn() + chronometer.start(printTime); + } else { + setStartBtn() + setResetBtn() + chronometer.stop(); + } }); // Reset/Split Button btnRightElement.addEventListener('click', () => { - // ... your code goes here -}); + if (btnLeftElement.classList.value === 'btn stop') { + printSplit() + } else { + chronometer.reset(clearSplits) + } +}); \ No newline at end of file