Last active
July 9, 2020 19:24
-
-
Save caprosset/a94c2ccb261ad51941570d1dfdc79c21 to your computer and use it in GitHub Desktop.
LAB | JS IronChronometer - Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Chronometer { | |
constructor() { | |
this.currentTime = 0; | |
this.intervalId = 0; | |
// BONUS => | |
this.millisecondsIntervalId = 0; | |
this.currentMilliseconds = 0; | |
} | |
startClick(printTime, /* BONUS => */ printMilliseconds) { | |
this.intervalId = setInterval( () => { | |
this.currentTime++; | |
if (printTime) printTime(); | |
}, 1000); | |
// BONUS => | |
this.millisecondsIntervalId = setInterval( () => { | |
if (this.currentMilliseconds === 99) { | |
this.currentMilliseconds = 0; | |
} | |
this.currentMilliseconds++; | |
if (printMilliseconds) printMilliseconds(); | |
}, 10); | |
} | |
getMinutes() { | |
let currentTimeMin = Math.floor(this.currentTime / 60); | |
return currentTimeMin; | |
} | |
getSeconds() { | |
let currentTimeSec = this.currentTime % 60; | |
return currentTimeSec; | |
} | |
twoDigitsNumber(num) { | |
return ("0" + num).slice(-2); | |
} | |
stopClick() { | |
clearInterval(this.intervalId); | |
clearInterval(this.millisecondsIntervalId); // <= BONUS | |
} | |
resetClick() { | |
this.currentTime = 0; | |
// BONUS => | |
this.currentMilliseconds = 0; | |
} | |
splitClick() { | |
let minutes = this.twoDigitsNumber(this.getMinutes()); | |
let seconds = this.twoDigitsNumber(this.getSeconds()); | |
let milliseconds = this.twoDigitsNumber(this.currentMilliseconds); // <= BONUS | |
return `${minutes}:${seconds}`; | |
// return `${minutes}:${seconds}:${milliseconds}`; // <= BONUS (comentado porque no pasa los tests sino) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chronometer = new Chronometer(); | |
// get the buttons: | |
const btnLeft = document.getElementById('btnLeft'); | |
const btnRight = document.getElementById('btnRight'); | |
// get the DOM elements that will serve us to display the time: | |
let minDec = document.getElementById('minDec'); | |
let minUni = document.getElementById('minUni'); | |
let secDec = document.getElementById('secDec'); | |
let secUni = document.getElementById('secUni'); | |
let milDec = document.getElementById('milDec'); | |
let milUni = document.getElementById('milUni'); | |
let splits = document.getElementById('splits'); | |
function printTime() { | |
printMinutes(); | |
printSeconds(); | |
} | |
function printMinutes() { | |
let minutes = chronometer.twoDigitsNumber(chronometer.getMinutes()); | |
minDec.innerHTML = minutes[0]; | |
minUni.innerHTML = minutes[1]; | |
} | |
function printSeconds() { | |
let seconds = chronometer.twoDigitsNumber(chronometer.getSeconds()); | |
secDec.innerHTML = seconds[0]; | |
secUni.innerHTML = seconds[1]; | |
} | |
// BONUS => | |
function printMilliseconds() { | |
let milliseconds = chronometer.twoDigitsNumber(chronometer.currentMilliseconds); | |
milDec.innerHTML = milliseconds[0]; | |
milUni.innerHTML = milliseconds[1]; | |
} | |
function printSplit() { | |
let newLi = document.createElement('li'); | |
newLi.className = "list-item"; | |
newLi.innerHTML = chronometer.splitClick(); | |
splits.appendChild(newLi); | |
} | |
function clearSplits() { | |
splits.innerHTML = ""; | |
} | |
function setStopBtn() { | |
btnLeft.className = 'btn stop'; | |
btnLeft.innerHTML = 'STOP'; | |
} | |
function setSplitBtn() { | |
btnRight.className = 'btn split'; | |
btnRight.innerHTML = 'SPLIT'; | |
} | |
function setStartBtn() { | |
btnLeft.className = 'btn start'; | |
btnLeft.innerHTML = 'START'; | |
} | |
function setResetBtn() { | |
btnRight.className = 'btn reset'; | |
btnRight.innerHTML = 'RESET'; | |
} | |
// Start/Stop Button | |
btnLeft.addEventListener('click', () => { | |
if(btnLeft.classList.contains('start')) { | |
chronometer.startClick(printTime, /* BONUS => */ printMilliseconds); | |
setStopBtn(); | |
setSplitBtn(); | |
} else { | |
chronometer.stopClick(); | |
setStartBtn(); | |
setResetBtn(); | |
} | |
}); | |
// Reset/Split Button | |
btnRight.addEventListener('click', () => { | |
if(btnRight.classList.contains('reset')) { | |
chronometer.resetClick(); | |
clearSplits(); | |
printTime(); | |
printMilliseconds(); // <= BONUS | |
} else { | |
printSplit(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment