Skip to content

Navin Raj - Internship #72

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

start(callback) {
// ... your code goes here
this.intervalId = setInterval(() => {
this.currentTime++; // Increment by 1 each second
}, 1000);
}

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

getSeconds() {
// ... your code goes here
return this.currentTime % 60;
}

getMilliseconds() {
return Math.floor((this.currentTime % 1000) / 10); // Convert to two-digit milliseconds
}

computeTwoDigitNumber(value) {
// ... your code goes here
return value.toString().padStart(2, '0');
}

stop() {
// ... your code goes here
clearInterval(this.intervalId);
}

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

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

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

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

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

function printSeconds() {
// ... your code goes here
const seconds = chronometer.computeTwoDigitNumber(chronometer.getSeconds());
secDec.innerHTML = seconds[0];
secUni.innerHTML = seconds[1];
}

// ==> BONUS
function printMilliseconds() {
// ... your code goes here
const milliseconds = chronometer.computeTwoDigitNumber(chronometer.getMilliseconds());
milDec.innerHTML = milliseconds[0];
milUni.innerHTML = milliseconds[1];
}

function printSplit() {
// ... your code goes here
const splitTime = chronometer.split();
const li = document.createElement('li');
li.innerHTML = splitTime;
splits.appendChild(li);
}

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

function setStopBtn() {
// ... your code goes here
btnLeft.classList.replace('start', 'stop');
btnLeft.innerHTML = 'STOP';
}

function setSplitBtn() {
// ... your code goes here
btnRight.classList.replace('reset', 'split');
btnRight.innerHTML = 'SPLIT';
}

function setStartBtn() {
// ... your code goes here
btnLeft.classList.replace('stop', 'start');
btnLeft.innerHTML = 'START';
}

function setResetBtn() {
// ... your code goes here
btnRight.classList.replace('split', 'reset');
btnRight.innerHTML = 'RESET';
}

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

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


});