From 0b316f415610b5238a84214c2af140f598d3f821 Mon Sep 17 00:00:00 2001 From: Jen-Winter <84963002+Jen-Winter@users.noreply.github.com> Date: Wed, 4 Jun 2025 02:50:19 +0200 Subject: [PATCH] lab solved --- javascript/chronometer.js | 25 ++++++++++++-------- javascript/index.js | 48 +++++++++++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/javascript/chronometer.js b/javascript/chronometer.js index 7a1349680..0a8ee3de4 100644 --- a/javascript/chronometer.js +++ b/javascript/chronometer.js @@ -1,39 +1,44 @@ 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 value < 10 ? `0${value}` : `${value}`; } 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}`; } } -// The following is required to make unit tests work. -/* Environment setup. Do not modify the below code. */ +// Required for tests if (typeof module !== 'undefined') { module.exports = Chronometer; } diff --git a/javascript/index.js b/javascript/index.js index fb3a43ab4..b8f0db395 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -14,15 +14,20 @@ 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.computeTwoDigitNumber(chronometer.getMinutes()); + minDecElement.innerText = minutes[0]; + minUniElement.innerText = minutes[1]; } function printSeconds() { - // ... your code goes here + const seconds = chronometer.computeTwoDigitNumber(chronometer.getSeconds()); + secDecElement.innerText = seconds[0]; + secUniElement.innerText = seconds[1]; } // ==> BONUS @@ -31,35 +36,60 @@ function printMilliseconds() { } function printSplit() { - // ... your code goes here + const li = document.createElement('li'); + li.className = 'list-item'; + li.innerText = chronometer.split(); + splitsElement.appendChild(li); } function clearSplits() { - // ... your code goes here + splitsElement.innerHTML = ''; + printTime(); // Reset time display to 00:00 } 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.classList.contains('start')) { + // Start the chronometer + chronometer.start(printTime); + setStopBtn(); + setSplitBtn(); + } else { + // Stop the chronometer + chronometer.stop(); + setStartBtn(); + setResetBtn(); + } }); // Reset/Split Button btnRightElement.addEventListener('click', () => { // ... your code goes here + if (btnRightElement.classList.contains('reset')) { + chronometer.reset(); + clearSplits(); + } else { + printSplit(); + } });