From fad4893630849ef4bfc35d376c719aae318e0237 Mon Sep 17 00:00:00 2001 From: SwaeZi Date: Thu, 4 Jul 2024 13:35:46 +0200 Subject: [PATCH] solve iteration x, y, z --- javascript/chronometer.js | 31 +++++++++++++----- javascript/index.js | 69 +++++++++++++++++++++++++++++++-------- 2 files changed, 79 insertions(+), 21 deletions(-) diff --git a/javascript/chronometer.js b/javascript/chronometer.js index 7a1349680..b99200a75 100644 --- a/javascript/chronometer.js +++ b/javascript/chronometer.js @@ -1,34 +1,49 @@ class Chronometer { constructor() { - // ... your code goes here + this.currentTime = 0; + this.intervalId = null; } start(callback) { - // ... your code goes here + if (!this.intervalId) { + 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; + } + + getMilliseconds() { + return 0; } computeTwoDigitNumber(value) { - // ... your code goes here + return value < 10 ? `0${value}` : `${value}`; } stop() { - // ... your code goes here + clearInterval(this.intervalId); + this.intervalId = null; } 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 fb3a43ab4..9b492db72 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -13,53 +13,96 @@ const milDecElement = document.getElementById('milDec'); const milUniElement = document.getElementById('milUni'); const splitsElement = document.getElementById('splits'); +let intervalId = null; + function printTime() { - // ... your code goes here + clearInterval(intervalId); + intervalId = setInterval(() => { + let seconds = printSeconds(); + let minutes = printMinutes(); + + minDecElement.innerText = minutes[0]; + minUniElement.innerText = minutes[1]; + secDecElement.innerText = seconds[0]; + secUniElement.innerText = seconds[1]; + }, 1000); } function printMinutes() { - // ... your code goes here + return chronometer.computeTwoDigitNumber(chronometer.getMinutes()); } function printSeconds() { - // ... your code goes here + return chronometer.computeTwoDigitNumber(chronometer.getSeconds()); } -// ==> BONUS +// BONUS: Implementing milliseconds display function printMilliseconds() { - // ... your code goes here + let milliseconds = chronometer.getMilliseconds(); + milDecElement.innerText = Math.floor(milliseconds / 10); + milUniElement.innerText = milliseconds % 10; } function printSplit() { - // ... your code goes here + let timestamp = chronometer.split(); + + let listItemElement = document.createElement('li'); + listItemElement.innerText = timestamp; + splitsElement.appendChild(listItemElement); } function clearSplits() { - // ... your code goes here + splitsElement.innerHTML = ''; } function setStopBtn() { - // ... your code goes here + btnLeftElement.innerText = 'STOP'; + btnLeftElement.className = 'btn stop'; } function setSplitBtn() { - // ... your code goes here + btnRightElement.innerText = 'SPLIT'; + btnRightElement.className = 'btn split'; } function setStartBtn() { - // ... your code goes here + btnLeftElement.innerText = 'START'; + btnLeftElement.className = 'btn start'; } function setResetBtn() { - // ... your code goes here + btnRightElement.innerText = 'RESET'; + btnRightElement.className = 'btn reset'; } // Start/Stop Button btnLeftElement.addEventListener('click', () => { - // ... your code goes here + if (btnLeftElement.innerText === 'START') { + setStopBtn(); + chronometer.start(); + printTime(); + setSplitBtn(); + } else { + setStartBtn(); + setResetBtn(); + chronometer.stop(); + clearInterval(intervalId); + } }); // Reset/Split Button btnRightElement.addEventListener('click', () => { - // ... your code goes here + if (btnRightElement.innerText === 'RESET') { + chronometer.stop(); + chronometer.reset(); + minDecElement.innerText = '0'; + minUniElement.innerText = '0'; + secDecElement.innerText = '0'; + secUniElement.innerText = '0'; + milDecElement.innerText = '0'; + milUniElement.innerText = '0'; + clearSplits(); + } else { + printSplit(); + } });