From 6ec471067d100df486bc6d12207d08d13abcde47 Mon Sep 17 00:00:00 2001 From: Ramiro Date: Fri, 16 May 2025 07:46:48 +0200 Subject: [PATCH] Lab terminado --- index.html | 2 ++ javascript/chronometer.js | 26 +++++++++++++------ javascript/index.js | 53 ++++++++++++++++++++++++++++++--------- 3 files changed, 61 insertions(+), 20 deletions(-) diff --git a/index.html b/index.html index 3415d7385..b9974881c 100644 --- a/index.html +++ b/index.html @@ -41,3 +41,5 @@

Splits

+ + diff --git a/javascript/chronometer.js b/javascript/chronometer.js index 7a1349680..4368db5e6 100644 --- a/javascript/chronometer.js +++ b/javascript/chronometer.js @@ -1,34 +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 += 1; + if (callback) callback(); + }, 10); } getMinutes() { - // ... your code goes here + return Math.floor(this.currentTime / 6000); } getSeconds() { - // ... your code goes here + return Math.floor(this.currentTime / 100) % 60; + } + + getMilliseconds () { + return this.currentTime % 100; } computeTwoDigitNumber(value) { - // ... your code goes here + return value.toString().padStart(2, '0'); } 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}`; } } diff --git a/javascript/index.js b/javascript/index.js index fb3a43ab4..4937b4759 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -14,52 +14,81 @@ const milUniElement = document.getElementById('milUni'); const splitsElement = document.getElementById('splits'); function printTime() { - // ... your code goes here + printMinutes(); + printSeconds(); + printMilliseconds(); } 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 function printMilliseconds() { - // ... your code goes here + const ms = chronometer.computeTwoDigitNumber(chronometer.getMilliseconds()); + milDecElement.innerText = ms[0]; + milUniElement.innerText = ms[1]; } 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 = ''; } function setStopBtn() { - // ... your code goes here + btnLeftElement.className = 'btn stop'; + btnLeftElement.innerText = 'STOP'; } function setSplitBtn() { - // ... your code goes here + btnRightElement.className = 'btn split'; + btnRightElement.innerText = 'SPLIT'; } function setStartBtn() { - // ... your code goes here + btnLeftElement.className = 'btn start'; + btnLeftElement.innerText = 'START'; } function setResetBtn() { - // ... your code goes here + btnRightElement.className = 'btn reset'; + btnRightElement.innerText = '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(); + printTime(); + clearSplits(); + } else { + printSplit(); + } });