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
22 changes: 20 additions & 2 deletions javascript/chronometer.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,55 @@
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
let formattedNumber = (value < 10) ? '0' + value.toString() : value.toString();
return formattedNumber;
}

stop() {
// ... your code goes here
if (this.intervalId !== null) {
clearInterval(this.intervalId);
this.intervalId = null;
}
clearInterval(this.intervalId)
}

reset() {
// ... your code goes here
this.currentTime = 0;
}

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

// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */
if (typeof module !== 'undefined') {
Expand Down
43 changes: 43 additions & 0 deletions javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ const splitsElement = document.getElementById('splits');

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

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

function printSeconds() {
// ... your code goes here
let second = chronometer.computeTwoDigitNumber(chronometer.getSeconds());
secDecElement.innerHTML = second[0];
secUniElement.innerHTML = second[1];
}

// ==> BONUS
Expand All @@ -32,34 +40,69 @@ function printMilliseconds() {

function printSplit() {
// ... your code goes here
let split = chronometer.split();
let li = document.createElement('li');
li.className = 'split-list';
li.innerHTML = split;
splitsElement.appendChild(li);
}

function clearSplits() {
// ... your code goes here
splitsElement.innerHTML = '';
}

function setStopBtn() {
// ... your code goes here
btnLeftElement.innerHTML = 'STOP';
btnLeftElement.classList.remove('start');
btnLeftElement.classList.add('stop');
}

function setSplitBtn() {
// ... your code goes here
btnRightElement.innerHTML = 'SPLIT';
btnRightElement.className = 'btn split';
}

function setStartBtn() {
// ... your code goes here
btnLeftElement.innerHTML = 'START';
btnLeftElement.className = 'btn start';
}

function setResetBtn() {
// ... your code goes here
btnRightElement.innerHTML = 'RESET';
btnRightElement.className = 'btn reset';
}

// Start/Stop Button
btnLeftElement.addEventListener('click', () => {
// ... your code goes here
console.log(btnLeftElement.className);
if (btnLeftElement.className === 'btn start') {
chronometer.start(printTime);
setStopBtn();
setSplitBtn();
}
else {
chronometer.stop();
setStartBtn();
setResetBtn();
}
});

// Reset/Split Button
btnRightElement.addEventListener('click', () => {
// ... your code goes here
console.log(btnRightElement.className);
if (btnRightElement.className === 'btn reset') {
chronometer.reset();
printTime();
clearSplits();
}
else {
printSplit();
}
});