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

start(callback) {
// ... your code goes here
if (!this.intervalId) {
this.intervalId = setInterval(() => {
this.currentTime += 1
}, 1000)
}
}

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

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

computeTwoDigitNumber(value) {
// ... your code goes here
if (value >= 10) {
return value.toString();
} else {
return ('0' + value).toString()
}
}

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

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

split() {
// ... your code goes here
if (this.currentTime >= 60) {
let minutesNum = this.getMinutes();
let secondsNum = this.getSeconds();
let minutes = this.computeTwoDigitNumber(minutesNum);
let seconds = this.computeTwoDigitNumber(secondsNum);
return `${minutes}:${seconds}`
} else {
let secondsNum = this.getSeconds();
let seconds = this.computeTwoDigitNumber(secondsNum);
return `00:${seconds}`
}
}
}

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

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

}

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

}

function printSeconds() {
Expand Down Expand Up @@ -57,6 +59,26 @@ function setResetBtn() {
// Start/Stop Button
btnLeftElement.addEventListener('click', () => {
// ... your code goes here
if (btnLeftElement.classList.contains('start')) {
btnLeftElement.classList.remove('start');
btnLeftElement.classList.add('stop');
btnLeftElement.innerHTML = 'STOP';
btnLeftElement.classList.remove('reset');
btnLeftElement.classList.add('split');
btnRightElement.innerHTML = 'SPLIT';

chronometer.start();

} else if (btnLeftElement.classList.contains('stop')) {
btnLeftElement.classList.remove('stop');
btnLeftElement.classList.add('start')
btnLeftElement.innerHTML = 'START';
btnRightElement.classList.remove('split');
btnRightElement.classList.add('reset');
btnRightElement.innerHTML = 'RESET';

chronometer.stop();
}
});

// Reset/Split Button
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test:watch": "jest --watchAll --verbose=false"
},
"devDependencies": {
"jest": "^26.6.3",
"jest": "^29.7.0",
"jest-html-reporter": "^3.3.0",
"jest-junit": "^12.0.0"
},
Expand Down