Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions javascript/chronometer.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
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(); // cada 10 ms se ejecuta el callback (la función que le pasemos)
}
}, 10); // Cada 10 ms incrementa el cronómetro (el update)
}

getMinutes() {
// ... your code goes here
return Math.floor(this.currentTime / 6000); // 6000 ms = 1 minuto
}

getSeconds() {
// ... your code goes here
return Math.floor((this.currentTime / 100) % 60); // 100 ms = 1 segundo
}

computeTwoDigitNumber(value) {
// ... your code goes here
getMilliseconds() {
return this.currentTime % 100; // El resto son los ms
}

stop() {
// ... your code goes here
computeTwoDigitNumber(value) {
return value.toString().padStart(2, '0'); // Asegura que siempre tenga 2 dígitos (1 = 01, ...)
}

reset() {
// ... your code goes here
this.currentTime = 0; // Reinicia el cronómetro a 0
}

stop() {
clearInterval(this.intervalId);
}

split() {
// ... your code goes here
return `${this.computeTwoDigitNumber(this.getMinutes())}
:${this.computeTwoDigitNumber(this.getSeconds())}
:${this.computeTwoDigitNumber(this.getMilliseconds())}`;
// Es extraña la línea pero devolvemos un string con el formato MM:SS:MS, siempre en el formate de 2 dígitos
}
}

Expand Down
58 changes: 46 additions & 12 deletions javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,86 @@ const milUniElement = document.getElementById('milUni');
const splitsElement = document.getElementById('splits');

function printTime() {
// ... your code goes here
printMinutes();
printSeconds();
printMilliseconds(); // Bonus
}

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 milliseconds = chronometer.computeTwoDigitNumber(
chronometer.getMilliseconds()
);

milDecElement.innerText = milliseconds[0];
milUniElement.innerText = milliseconds[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.innerText = ''; // Limpia el contenido de la lista de elementos
}

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();
}
});