From cf70d523c8a0aa8c308cb4b2198a8ca75e35b5d8 Mon Sep 17 00:00:00 2001 From: AidaG91 Date: Thu, 3 Jul 2025 13:45:42 +0200 Subject: [PATCH 1/3] Interation 1 --- javascript/chronometer.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/javascript/chronometer.js b/javascript/chronometer.js index 7a1349680..0627b572f 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 + 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 `${minutesFormat}:${secondsFormat}`; } } From 595e4c871d535802ef2adb055dad9f525392af51 Mon Sep 17 00:00:00 2001 From: AidaG91 Date: Thu, 3 Jul 2025 14:57:54 +0200 Subject: [PATCH 2/3] Iteration 2 --- javascript/chronometer.js | 5 ++-- javascript/index.js | 49 +++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/javascript/chronometer.js b/javascript/chronometer.js index 0627b572f..83237fbb3 100644 --- a/javascript/chronometer.js +++ b/javascript/chronometer.js @@ -5,9 +5,8 @@ class Chronometer { } start(callback) { - this.intervalId = setInterval(() => { + this.intervalId = setInterval(() => { this.currentTime++; - if (typeof callback === 'function') { callback(); } @@ -43,7 +42,7 @@ class Chronometer { const formattedMinutes = this.computeTwoDigitNumber(minutes); const formattedSeconds = this.computeTwoDigitNumber(seconds); - return `${minutesFormat}:${secondsFormat}`; + return `${formattedMinutes}:${formattedSeconds}`; } } diff --git a/javascript/index.js b/javascript/index.js index fb3a43ab4..6aa165d30 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,7 +38,10 @@ 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() { @@ -39,27 +49,46 @@ function clearSplits() { } 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(); + } else { + chronometer.split(); + printSplit(); + } }); From 0e6f16cff674e982fc311c94ffa591c9eae3dc9f Mon Sep 17 00:00:00 2001 From: AidaG91 Date: Thu, 3 Jul 2025 15:07:20 +0200 Subject: [PATCH 3/3] Iteration 4 --- javascript/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/javascript/index.js b/javascript/index.js index 6aa165d30..c5d9c77b6 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -45,7 +45,7 @@ function printSplit() { } function clearSplits() { - // ... your code goes here + splitsElement.innerHTML = ''; } function setStopBtn() { @@ -87,6 +87,7 @@ btnRightElement.addEventListener('click', () => { chronometer.reset(); printMinutes(); printSeconds(); + clearSplits(); } else { chronometer.split(); printSplit();