Skip to content

solved iterations x,y,z #86

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 1 commit 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
30 changes: 15 additions & 15 deletions javascript/chronometer.js
Original file line number Diff line number Diff line change
@@ -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
computeTwoDigitNumber(num) {
return num < 10 ? `0${num}` : `${num}`;
}

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;
}
92 changes: 33 additions & 59 deletions javascript/index.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,39 @@
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 splitsList = document.getElementById('splits');

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

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

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

// ==> BONUS
function printMilliseconds() {
// ... your code goes here
}

function printSplit() {
// ... your code goes here
}

function clearSplits() {
// ... your code goes here
}

function setStopBtn() {
// ... your code goes here
}

function setSplitBtn() {
// ... your code goes here
}

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

function setResetBtn() {
// ... your code goes here
}

// Start/Stop Button
btnLeftElement.addEventListener('click', () => {
// ... your code goes here
const minutes = chronometer.computeTwoDigitNumber(chronometer.getMinutes());
const seconds = chronometer.computeTwoDigitNumber(chronometer.getSeconds());
document.getElementById('time').innerHTML = `${minutes}:${seconds}`;
}

btnLeft.addEventListener('click', () => {
if (btnLeft.classList.contains('start')) {
chronometer.start(printTime);
btnLeft.classList.replace('start', 'stop');
btnLeft.innerHTML = 'STOP';
btnRight.classList.replace('reset', 'split');
btnRight.innerHTML = 'SPLIT';
} else {
chronometer.stop();
btnLeft.classList.replace('stop', 'start');
btnLeft.innerHTML = 'START';
btnRight.classList.replace('split', 'reset');
btnRight.innerHTML = 'RESET';
}
});

// Reset/Split Button
btnRightElement.addEventListener('click', () => {
// ... your code goes here
btnRight.addEventListener('click', () => {
if (btnRight.classList.contains('split')) {
const splitItem = document.createElement('li');
splitItem.innerHTML = chronometer.split();
splitItem.classList.add('list-item');
splitsList.appendChild(splitItem);
} else {
chronometer.reset();
document.getElementById('time').innerHTML = '00:00';
splitsList.innerHTML = ''; // Clear all splits
}
});