diff --git a/javascript/chronometer.js b/javascript/chronometer.js index 7a1349680..7f886ec1a 100644 --- a/javascript/chronometer.js +++ b/javascript/chronometer.js @@ -1,34 +1,55 @@ class Chronometer { constructor() { // ... your code goes here - } + this.currentTime= 0; + this.intervalId= null; + + } + start(callback) { - // ... your code goes here + this.intervalId= setInterval(()=> { + this.currentTime= this.currentTime+1 ; + }, 1000); + callback(this.intervalId) + } getMinutes() { // ... your code goes here + const minutes= Math.floor (this.currentTime/60); + return minutes } - getSeconds() { // ... your code goes here + const seconds= Math.floor(this.currentTime%60); + return seconds } computeTwoDigitNumber(value) { + let twoDigits = value.toString(); + if (value.length < 2) { + numStr = '0' + numStr; + } + return twoDigits; // ... your code goes here } stop() { // ... your code goes here + clearInterval(this.intervalId); + this.intervalId= null; } reset() { // ... your code goes here + this.currentTime=0 } split() { + let split= (this.getMinutes()) + ":"+ (this.getSeconds()) // ... your code goes here + return split } } diff --git a/javascript/index.js b/javascript/index.js index fb3a43ab4..4da91eb1f 100644 --- a/javascript/index.js +++ b/javascript/index.js @@ -18,15 +18,12 @@ function printTime() { } function printMinutes() { + minUniElement.innerText = chronometer.getMinutes() // ... your code goes here } function printSeconds() { - // ... your code goes here -} - -// ==> BONUS -function printMilliseconds() { + secUniElement.innerText = chronometer.getSeconds() // ... your code goes here } @@ -47,6 +44,7 @@ function setSplitBtn() { } function setStartBtn() { + chronometer.start() // ... your code goes here } @@ -56,6 +54,18 @@ function setResetBtn() { // Start/Stop Button btnLeftElement.addEventListener('click', () => { + const leftButton = document.getElementById("btnLeft"); + const rightButton = document.getElementById("btnRight"); + if (leftButton.innerHTML === "START") { + btnLeftElement.addEventListener('click', chronometer.start()); + leftButton.innerHTML = "STOP"; + rightButton.innerHTML = "SLICE"; + } else { + btnLeftElement.addEventListener('click', setStopBtn); + leftButton.innerHTML = "START"; + rightButton.innerHTML = "RESET"; + } + // ... your code goes here }); @@ -63,3 +73,5 @@ btnLeftElement.addEventListener('click', () => { btnRightElement.addEventListener('click', () => { // ... your code goes here }); + +