Skip to content

Solved lab #1943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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: 32 additions & 1 deletion javascript/chronometer.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,70 @@
class Chronometer {
constructor() {
// ... your code goes here
this.currentTime = 0;
this.intervalID = null;
}

start(callback) {
// ... your code goes here
console.log("⏳ start() was called!");
this.intervalID = setInterval(() => {
this.currentTime++;
if (callback) { // esto hace que se repita cada segundo la suma de segundos
callback();
}
console.log(`🔢 currentTime: ${this.currentTime}`);
}, 1000);
}



getMinutes() {
console.log("⏳ getminutes() was called!"); // DEBUG

// ... your code goes here
return Math.floor(this.currentTime / 60);
}

getSeconds() {
// ... your code goes here
console.log("⏳ getseconds() was called!"); // DEBUG
return this.currentTime%60;
}

computeTwoDigitNumber(value) {
// ... your code goes here
console.log("⏳ twodigit() was called!"); // DEBUG
return ('0' + value).slice(-2);
}

stop() {
// ... your code goes here
this.currentTime = 0;
clearInterval(this.intervalID)
}

reset() {
// ... your code goes here
this.currentTime = 0;
document.querySelector("#sphere > span").innerHTML = "0";
}

split() {
// ... your code goes here
const minutes = this.computeTwoDigitNumber(this.getMinutes());
const seconds = this.computeTwoDigitNumber(this.getSeconds());
return `${minutes}:${seconds}`;
}
}





// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */
if (typeof module !== 'undefined') {
module.exports = Chronometer;
}


56 changes: 56 additions & 0 deletions javascript/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const chronometer = new Chronometer();
const secondChronometer = new Chronometer();

// get the buttons:
const btnLeftElement = document.getElementById('btnLeft');
Expand All @@ -15,14 +16,23 @@ const splitsElement = document.getElementById('splits');

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

function printMinutes() {
// ... your code goes here
const minutes = chronometer.computeTwoDigitNumber(chronometer.getMinutes());
minDecElement.innerHTML = minutes[0];
minUniElement.innerHTML = minutes[1];

}

function printSeconds() {
// ... your code goes here
const seconds = chronometer.computeTwoDigitNumber(chronometer.getSeconds());
secDecElement.textContent = seconds[0];
secUniElement.textContent = seconds[1];
}

// ==> BONUS
Expand All @@ -32,6 +42,10 @@ function printMilliseconds() {

function printSplit() {
// ... your code goes here
const splitTime = chronometer.split();
const newSplit = document.createElement("li");
newSplit.textContent = splitTime;
splitsElement.appendChild(newSplit);
}

function clearSplits() {
Expand All @@ -48,6 +62,7 @@ function setSplitBtn() {

function setStartBtn() {
// ... your code goes here

}

function setResetBtn() {
Expand All @@ -57,9 +72,50 @@ function setResetBtn() {
// Start/Stop Button
btnLeftElement.addEventListener('click', () => {
// ... your code goes here
if(btnLeft.classList.contains("start"))
{
chronometer.start(printTime);
btnLeftElement.classList.remove('start');
btnLeftElement.classList.add('stop');
btnLeftElement.textContent = 'STOP';
btnRightElement.classList.remove('stop');
btnRightElement.classList.add('split');
btnRightElement.textContent = 'Split';
} else if(btnLeft.classList.contains("stop"))
{
chronometer.stop();
btnLeftElement.classList.remove('stop');
btnLeftElement.classList.add('start');
btnLeftElement.textContent = 'START';
btnRightElement.classList.remove('split');
btnRightElement.classList.add('reset');
btnRightElement.textContent = 'RESET';

const splitsElement = document.getElementById("splits");
splitsElement.innerHTML=" "

minDecElement.innerHTML = 0;
minUniElement.innerHTML = 0;
secDecElement.textContent = 0;
secUniElement.textContent = 0;
}
});

// Reset/Split Button
btnRightElement.addEventListener('click', () => {
// ... your code goes here
if(btnRight.classList.contains('split'))
{
chronometer.split();
const newListItem = document.createElement("li");
newListItem.className = "list-item";
newListItem.innerHTML = chronometer.split();
const splitsElement = document.getElementById("splits");
splitsElement.appendChild(newListItem);
}else if(btnRightElement.classList.contains("reset"))
{
clearSplits();
chronometer.reset();
}

});