diff --git a/index.html b/index.html
index 3415d7385..59c5faedb 100644
--- a/index.html
+++ b/index.html
@@ -4,7 +4,7 @@
-
IronChronometer
+ IronChronometer
diff --git a/javascript/chronometer.js b/javascript/chronometer.js
index 7a1349680..62591c255 100644
--- a/javascript/chronometer.js
+++ b/javascript/chronometer.js
@@ -1,37 +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++;
+ if (callback) callback();
+ }, 1000);
}
getMinutes() {
- // ... your code goes here
+ return Math.floor(this.currentTime / 60);
}
getSeconds() {
- // ... your code goes here
+ return this.currentTime % 60;
}
computeTwoDigitNumber(value) {
- // ... your code goes here
+ return value < 10 ? `0${value}` : `${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());
+ return `${minutes}:${seconds}`;
}
}
+
// The following is required to make unit tests work.
/* Environment setup. Do not modify the below code. */
if (typeof module !== 'undefined') {
diff --git a/javascript/index.js b/javascript/index.js
index fb3a43ab4..eabfbb5c3 100644
--- a/javascript/index.js
+++ b/javascript/index.js
@@ -13,53 +13,72 @@ const milDecElement = document.getElementById('milDec');
const milUniElement = document.getElementById('milUni');
const splitsElement = document.getElementById('splits');
+
function printTime() {
- // ... your code goes here
+ printMinutes();
+ printSeconds();
}
function printMinutes() {
- // ... your code goes here
+ const minutes = chronometer.computeTwoDigitNumber(chronometer.getMinutes());
+ minDec.textContent = minutes[0];
+ minUni.textContent = minutes[1];
}
function printSeconds() {
- // ... your code goes here
-}
-
-// ==> BONUS
-function printMilliseconds() {
- // ... your code goes here
+ const seconds = chronometer.computeTwoDigitNumber(chronometer.getSeconds());
+ secDec.textContent = seconds[0];
+ secUni.textContent = seconds[1];
}
function printSplit() {
- // ... your code goes here
+ const li = document.createElement('li');
+ li.textContent = chronometer.split();
+ splits.appendChild(li);
}
function clearSplits() {
- // ... your code goes here
+ splits.innerHTML = '';
}
function setStopBtn() {
- // ... your code goes here
+ btnLeft.textContent = 'STOP';
+ btnLeft.classList.replace('start', 'stop');
}
function setSplitBtn() {
- // ... your code goes here
+ btnRight.textContent = 'SPLIT';
+ btnRight.classList.replace('reset', 'split');
}
function setStartBtn() {
- // ... your code goes here
+ btnLeft.textContent = 'START';
+ btnLeft.classList.replace('stop', 'start');
}
function setResetBtn() {
- // ... your code goes here
+ btnRight.textContent = 'RESET';
+ btnRight.classList.replace('split', 'reset');
}
-// Start/Stop Button
-btnLeftElement.addEventListener('click', () => {
- // ... your code goes here
+btnLeft.addEventListener('click', () => {
+ 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
+btnRight.addEventListener('click', () => {
+ if (btnRight.classList.contains('reset')) {
+ chronometer.reset();
+ clearSplits();
+ printTime();
+ } else {
+ printSplit();
+ }
});