Skip to content
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

Lab-Chronometer-Rahul #69

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
17 changes: 17 additions & 0 deletions javascript/chronometer.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,51 @@
class Chronometer {
constructor() {
// ... your code goes here
this.currentTime = 0;
this.intervalId = null;
}

start(callback) {
// ... your code goes here
this.intervalId = setInterval(() => {
if (callback && typeof callback === 'function') {
callback();
}
this.currentTime++;
}, 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
return ('00' + value).slice(-2);
}

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

Expand Down
36 changes: 36 additions & 0 deletions javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,87 @@ const splitsElement = document.getElementById('splits');

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

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

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

// ==> BONUS
function printMilliseconds() {
// ... your code goes here
// let ms = chronometer.computeTwoDigitNumber(chronometer.getMillieconds);
}

function printSplit() {
// ... your code goes here
let split = chronometer.split();
let liTag = document.createElement('li');
}

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

function setStopBtn() {
// ... your code goes here
btnLeftElement.innerHTML = 'STOP';
btnLeftElement.className = 'btn stop';
}

function setSplitBtn() {
// ... your code goes here
btnLeftElement.innerHTML = 'SPLIT';
btnLeftElement.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
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
if (btnRightElement.className === 'btn reset') {
chronometer.reset();
printTime();
clearSplits();
} else {
printSplit();
}
});