Skip to content

lab #1950

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

lab #1950

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
27 changes: 19 additions & 8 deletions javascript/chronometer.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
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();
}, 10);
}

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

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

getMilliseconds() {
return this.currentTime % 100;
}

computeTwoDigitNumber(value) {
// ... your code goes here
return value < 10 ? '0' + value : String(value);
}

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}.${milliseconds}`;
}
}

Expand Down
58 changes: 42 additions & 16 deletions javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,78 @@ const milUniElement = document.getElementById('milUni');
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());
minDecElement.innerText = minutes[0];
minUniElement.innerText = minutes[1];
}

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

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

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

function clearSplits() {
// ... your code goes here
const li = document.createElement('li');
li.className = 'list-item';
li.innerText = chronometer.split();
splitsElement.appendChild(li);
}

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

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

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

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

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

// Reset/Split Button
btnRightElement.addEventListener('click', () => {
// ... your code goes here
if (btnRightElement.classList.contains('reset')) {
chronometer.reset();
printTime();
clearSplits();
} else {
printSplit();
}
});
20 changes: 10 additions & 10 deletions tests/chronometer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,15 @@ describe('Chronometer', () => {

// If you decide to work on the bonus iteration,
// comment the previous test and uncomment the following
// it('should return valid format with minutes, seconds and milliseconds', () => {
// let min = chronometer.getMinutes();
// let sec = chronometer.getSeconds();
// let milli = chronometer.getMilliseconds();
// if (min < 10) {
// expect(chronometer.split()).toEqual(`0${min}:0${sec}:0${milli}`);
// } else {
// expect(chronometer.split()).toEqual(`${min}:${sec}:${milli}`);
// }
// });
it('should return valid format with minutes, seconds and milliseconds', () => {
let min = chronometer.getMinutes();
let sec = chronometer.getSeconds();
let milli = chronometer.getMilliseconds();
if (min < 10) {
expect(chronometer.split()).toEqual(`0${min}:0${sec}:0${milli}`);
} else {
expect(chronometer.split()).toEqual(`${min}:${sec}:${milli}`);
}
});
});
});