diff --git a/javascript/chronometer.js b/javascript/chronometer.js index 7a1349680..8f13ac8e8 100644 --- a/javascript/chronometer.js +++ b/javascript/chronometer.js @@ -1,34 +1,69 @@ 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}` + } } } @@ -36,4 +71,4 @@ class Chronometer { /* Environment setup. Do not modify the below code. */ if (typeof module !== 'undefined') { module.exports = Chronometer; -} +} \ No newline at end of file diff --git a/javascript/index.js b/javascript/index.js index fb3a43ab4..0927ed6de 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -15,10 +15,12 @@ const splitsElement = document.getElementById('splits'); function printTime() { // ... your code goes here + } function printMinutes() { // ... your code goes here + } function printSeconds() { @@ -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 diff --git a/package.json b/package.json index 879644e13..d6e8b15ed 100644 --- a/package.json +++ b/package.json @@ -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" },