From 463d59b06cfe06847192d83e2fac9efcee6a0181 Mon Sep 17 00:00:00 2001 From: saira-jerjes Date: Sun, 9 Mar 2025 11:58:05 +0100 Subject: [PATCH] solved lab --- SpecRunner.html | 36 +++++++++++++++++---------------- index.html | 12 +++++++---- src/chronometer-centiseconds.js | 26 ++++++++++++++++++++++++ src/chronometer.js | 20 ++++++++++++++++++ 4 files changed, 73 insertions(+), 21 deletions(-) diff --git a/SpecRunner.html b/SpecRunner.html index 0265dc18..f93f39a8 100644 --- a/SpecRunner.html +++ b/SpecRunner.html @@ -1,25 +1,27 @@ - - - Jasmine Spec Runner v2.8.0 - - + + + Jasmine Spec Runner v2.8.0 - - - + + - - - + + + - - - + + - + + + - - + + + + + \ No newline at end of file diff --git a/index.html b/index.html index 32e914c7..7959df84 100644 --- a/index.html +++ b/index.html @@ -1,5 +1,6 @@ + @@ -7,6 +8,7 @@ JS Chronometer +
@@ -19,7 +21,8 @@

Splits

- 00:00 + 00:00
00 @@ -38,13 +41,14 @@

Splits

- + - + + - + \ No newline at end of file diff --git a/src/chronometer-centiseconds.js b/src/chronometer-centiseconds.js index 8337b5b6..b9c8db30 100644 --- a/src/chronometer-centiseconds.js +++ b/src/chronometer-centiseconds.js @@ -1,37 +1,63 @@ class Chronometer { constructor() { // ... your code goes here + this.currentTime = 0; + this.intervalId = null; } start(printTimeCallback) { // ... your code goes here + this.intervalId = setInterval(() => { + this.currentTime += 1; + if (printTimeCallback) { + printTimeCallback(); + } + }, 10); } getMinutes() { // ... your code goes here + const minutes = Math.floor(this.currentTime / 6000); + return minutes; } getSeconds() { // ... your code goes here + const seconds = Math.floor((this.currentTime / 100) % 60); + return seconds; } getCentiseconds() { // ... your code goes here + const centiseconds = Math.floor(this.currentTime % 100); + return centiseconds; } computeTwoDigitNumber(value) { // ... your code goes here + if (value.toString().length < 2) { + return `0${value}`; + } else { + return value.toString(); + } } 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 centiseconds = this.computeTwoDigitNumber(this.getCentiseconds()); + + return `${minutes}:${seconds}.${centiseconds}`; } } diff --git a/src/chronometer.js b/src/chronometer.js index 83c75bd2..a833512d 100644 --- a/src/chronometer.js +++ b/src/chronometer.js @@ -1,33 +1,53 @@ class Chronometer { constructor() { // ... your code goes here + this.currentTime = 0; + this.intervalId = null; } start(printTimeCallback) { // ... your code goes here + this.intervalId = setInterval(() => { + this.currentTime += 1; + if (printTimeCallback) { + printTimeCallback(); + } + }, 1000); } getMinutes() { // ... your code goes here + return Math.floor(this.currentTime / 60); } getSeconds() { // ... your code goes here + return Math.floor(this.currentTime % 60); } computeTwoDigitNumber(value) { // ... your code goes here + if (value.toString().length < 2) { + return `0${value}`; + } else { + return value.toString(); + } } 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}`; } }