From 87ae8bf947b04311c343dd5fa5270d081af89f67 Mon Sep 17 00:00:00 2001 From: Laura Castillo Date: Tue, 13 May 2025 21:37:42 +0200 Subject: [PATCH] Solved-Lab --- javascript/chronometer.js | 28 +++++++------- javascript/index.js | 79 +++++++++++++++++++++++---------------- 2 files changed, 61 insertions(+), 46 deletions(-) diff --git a/javascript/chronometer.js b/javascript/chronometer.js index 7a1349680..8399204ea 100644 --- a/javascript/chronometer.js +++ b/javascript/chronometer.js @@ -1,39 +1,39 @@ 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.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 + return `${this.computeTwoDigitNumber( + this.getMinutes() + )}:${this.computeTwoDigitNumber(this.getSeconds())}`; } } - -// The following is required to make unit tests work. -/* Environment setup. Do not modify the below code. */ -if (typeof module !== 'undefined') { - module.exports = Chronometer; -} diff --git a/javascript/index.js b/javascript/index.js index fb3a43ab4..32295d3c3 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -1,65 +1,80 @@ const chronometer = new Chronometer(); -// get the buttons: -const btnLeftElement = document.getElementById('btnLeft'); -const btnRightElement = document.getElementById('btnRight'); - -// get the DOM elements that will serve us to display the time: -const minDecElement = document.getElementById('minDec'); -const minUniElement = document.getElementById('minUni'); -const secDecElement = document.getElementById('secDec'); -const secUniElement = document.getElementById('secUni'); -const milDecElement = document.getElementById('milDec'); -const milUniElement = document.getElementById('milUni'); -const splitsElement = document.getElementById('splits'); +const btnLeft = document.getElementById('btnLeft'); +const btnRight = document.getElementById('btnRight'); +const minDec = document.getElementById('minDec'); +const minUni = document.getElementById('minUni'); +const secDec = document.getElementById('secDec'); +const secUni = document.getElementById('secUni'); +const splits = document.getElementById('splits'); function printTime() { - // ... your code goes here + printMinutes(); + printSeconds(); } function printMinutes() { - // ... your code goes here + const minutes = chronometer.computeTwoDigitNumber(chronometer.getMinutes()); + minDec.innerText = minutes[0]; + minUni.innerText = minutes[1]; } function printSeconds() { - // ... your code goes here -} - -// ==> BONUS -function printMilliseconds() { - // ... your code goes here + const seconds = chronometer.computeTwoDigitNumber(chronometer.getSeconds()); + secDec.innerText = seconds[0]; + secUni.innerText = seconds[1]; } function printSplit() { - // ... your code goes here + const splitTime = chronometer.split(); + const li = document.createElement('li'); + li.innerText = splitTime; + li.className = 'list-item'; + splits.appendChild(li); } function clearSplits() { - // ... your code goes here + splits.innerHTML = ''; } function setStopBtn() { - // ... your code goes here + btnLeft.innerText = 'STOP'; + btnLeft.className = 'btn stop'; } function setSplitBtn() { - // ... your code goes here + btnRight.innerText = 'SPLIT'; + btnRight.className = 'btn split'; } function setStartBtn() { - // ... your code goes here + btnLeft.innerText = 'START'; + btnLeft.className = 'btn start'; } function setResetBtn() { - // ... your code goes here + btnRight.innerText = 'RESET'; + btnRight.className = 'btn reset'; } -// Start/Stop Button -btnLeftElement.addEventListener('click', () => { - // ... your code goes here +btnLeft.addEventListener('click', () => { + if (btnLeft.classList.contains('start')) { + chronometer.start(printTime); + setStopBtn(); + setSplitBtn(); + } else { + chronometer.stop(); + setStartBtn(); + setResetBtn(); + } }); -// Reset/Split Button -btnRightElement.addEventListener('click', () => { - // ... your code goes here +btnRight.addEventListener('click', () => { + if (btnRight.classList.contains('reset')) { + chronometer.reset(); + clearSplits(); + printTime(); + } else { + printSplit(); + } });