From 3d03d05e4020d0cd730448c04383b1a57244eef5 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Wed, 30 Sep 2026 10:35:34 +0100 Subject: [PATCH 1/7] first revision of the function - tried to include all conditions. --- format-clock-edge-cases/timeConverter.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9..203fde85b 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,11 +1,18 @@ function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); + const minutes = time.slice(-2); - if (hours > 12) { - return `${hours - 12}:00 pm`; + if (hours > 24 || hours < 0) { + return "invalid input."; + } else if (hours === 24 || hours === 0) { + return `${12}:${minutes}am`; + } else if (hours === 12) { + return `${hours}:${minutes}pm`; + } else if (hours > 12) { + return `${hours - 12}:${minutes}pm`; + } else { + return `${hours}:${minutes}am`; + } } - return `${time} am`; -} -export {formatAs12HourClock}; +export { formatAs12HourClock }; From cf5075f576f2f8b0c59519006d183a60d29c6d97 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Wed, 30 Sep 2026 10:55:35 +0100 Subject: [PATCH 2/7] modified the first confition in the function. include "-" --- format-clock-edge-cases/timeConverter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 203fde85b..d8813d79a 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -2,7 +2,7 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); const minutes = time.slice(-2); - if (hours > 24 || hours < 0) { + if (hours > 24 || hours < 0 || time.includes("-")) { return "invalid input."; } else if (hours === 24 || hours === 0) { return `${12}:${minutes}am`; From 23d7cfdd2b6573c2e4c935f51779ada4bcfe91a4 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Wed, 30 Sep 2026 12:16:57 +0100 Subject: [PATCH 3/7] modified the test.js --- format-clock-edge-cases/timeConverter.test.js | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 88f2af094..1573c433c 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -2,10 +2,31 @@ import {formatAs12HourClock} from "./timeConverter.js"; import assert from "node:assert"; import test from "node:test"; -test("correctly convert time after 12:00", function(){ - assert.equal(formatAs12HourClock("23:00"), "11:00 pm"); +test("can return 'invalid input.'", function(){ + assert.equal(formatAs12HourClock("25:00"), "invalid input."); }); -test("can correctly convert morning time", function() { - assert.equal(formatAs12HourClock("08:00"), "08:00 am"); +test("can return 'invalid input.'", function(){ + assert.equal(formatAs12HourClock("-01:00"), "invalid input."); +}) + +test("can correctly convert morning time 24:12", function() { + assert.equal(formatAs12HourClock("24:12"), "12:12am"); +}); + +test("can correctly convert morning time 00:12", function() { + assert.equal(formatAs12HourClock("00:12"), "12:12am"); +}); + +test("can correctly convert afternoon time 12:12", function() { + assert.equal(formatAs12HourClock("12:12"), "12:12pm"); }); + +test("can correctly convert afternoon time 15:30", function() { + assert.equal(formatAs12HourClock("15:30"), "3:30pm"); +}); + +test("can correctly convert morning time 03:45", function() { + assert.equal(formatAs12HourClock("03:45"), "3:45am"); +}); + From 050f6e640f8557c67bfdbb17b0d9461bf7681650 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Thu, 1 Oct 2026 22:15:00 +0100 Subject: [PATCH 4/7] modified tests according to Abdi's comments. --- format-clock-edge-cases/timeConverter.test.js | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 1573c433c..970c45ed4 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -2,31 +2,29 @@ import {formatAs12HourClock} from "./timeConverter.js"; import assert from "node:assert"; import test from "node:test"; -test("can return 'invalid input.'", function(){ - assert.equal(formatAs12HourClock("25:00"), "invalid input."); +test("correctly convert time afternoon time: 23:00", function(){ + assert.equal(formatAs12HourClock("23:00"), "11:00pm"); }); -test("can return 'invalid input.'", function(){ - assert.equal(formatAs12HourClock("-01:00"), "invalid input."); -}) - -test("can correctly convert morning time 24:12", function() { - assert.equal(formatAs12HourClock("24:12"), "12:12am"); +test("can correctly convert morning time 08:00", function() { + assert.equal(formatAs12HourClock("08:00"), "8:00am"); }); -test("can correctly convert morning time 00:12", function() { - assert.equal(formatAs12HourClock("00:12"), "12:12am"); +test("can correctly convert morning time 11:59", function() { + assert.equal(formatAs12HourClock("11:59"), "11:59am"); }); -test("can correctly convert afternoon time 12:12", function() { - assert.equal(formatAs12HourClock("12:12"), "12:12pm"); + test("can correctly convert afternoon time 23:59", function() { + assert.equal(formatAs12HourClock("23:159"), "11:59pm"); }); -test("can correctly convert afternoon time 15:30", function() { - assert.equal(formatAs12HourClock("15:30"), "3:30pm"); +test("can correctly convert morning time 00:00", function() { + assert.equal(formatAs12HourClock("00:00"), "12:00am"); }); -test("can correctly convert morning time 03:45", function() { - assert.equal(formatAs12HourClock("03:45"), "3:45am"); +test("can correctly convert afternoon time 12:00", function() { + assert.equal(formatAs12HourClock("12:00"), "12:00pm"); }); + + From 23c127dda792f0c85bae510c2f7639136ab4cfaa Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Thu, 1 Oct 2026 22:28:52 +0100 Subject: [PATCH 5/7] formatted documents --- format-clock-edge-cases/timeConverter.js | 2 +- format-clock-edge-cases/timeConverter.test.js | 29 +++++++++---------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index d8813d79a..bccf02ef6 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -12,7 +12,7 @@ function formatAs12HourClock(time) { return `${hours - 12}:${minutes}pm`; } else { return `${hours}:${minutes}am`; - } } +} export { formatAs12HourClock }; diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 970c45ed4..fdfa7d3f5 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -1,30 +1,27 @@ -import {formatAs12HourClock} from "./timeConverter.js"; +import { formatAs12HourClock } from "./timeConverter.js"; import assert from "node:assert"; import test from "node:test"; -test("correctly convert time afternoon time: 23:00", function(){ - assert.equal(formatAs12HourClock("23:00"), "11:00pm"); +test("correctly convert time afternoon time: 23:00", function () { + assert.equal(formatAs12HourClock("23:00"), "11:00pm"); }); -test("can correctly convert morning time 08:00", function() { - assert.equal(formatAs12HourClock("08:00"), "8:00am"); +test("can correctly convert morning time 08:00", function () { + assert.equal(formatAs12HourClock("08:00"), "8:00am"); }); -test("can correctly convert morning time 11:59", function() { - assert.equal(formatAs12HourClock("11:59"), "11:59am"); +test("can correctly convert morning time 11:59", function () { + assert.equal(formatAs12HourClock("11:59"), "11:59am"); }); - test("can correctly convert afternoon time 23:59", function() { - assert.equal(formatAs12HourClock("23:159"), "11:59pm"); +test("can correctly convert afternoon time 23:59", function () { + assert.equal(formatAs12HourClock("23:159"), "11:59pm"); }); -test("can correctly convert morning time 00:00", function() { - assert.equal(formatAs12HourClock("00:00"), "12:00am"); +test("can correctly convert morning time 00:00", function () { + assert.equal(formatAs12HourClock("00:00"), "12:00am"); }); -test("can correctly convert afternoon time 12:00", function() { - assert.equal(formatAs12HourClock("12:00"), "12:00pm"); +test("can correctly convert afternoon time 12:00", function () { + assert.equal(formatAs12HourClock("12:00"), "12:00pm"); }); - - - From 6836eaa769c681efdd2100ccfbfa0f3dfe0d163a Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Fri, 2 Oct 2026 22:44:44 +0100 Subject: [PATCH 6/7] modified the format of the result. Corrected then typo in test. --- format-clock-edge-cases/timeConverter.js | 8 ++++---- format-clock-edge-cases/timeConverter.test.js | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index bccf02ef6..26579811c 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -5,13 +5,13 @@ function formatAs12HourClock(time) { if (hours > 24 || hours < 0 || time.includes("-")) { return "invalid input."; } else if (hours === 24 || hours === 0) { - return `${12}:${minutes}am`; + return `${12}:${minutes} am`; } else if (hours === 12) { - return `${hours}:${minutes}pm`; + return `${hours}:${minutes} pm`; } else if (hours > 12) { - return `${hours - 12}:${minutes}pm`; + return `${hours - 12}:${minutes} pm`; } else { - return `${hours}:${minutes}am`; + return `${hours}:${minutes} am`; } } diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index fdfa7d3f5..e20861249 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -3,25 +3,25 @@ import assert from "node:assert"; import test from "node:test"; test("correctly convert time afternoon time: 23:00", function () { - assert.equal(formatAs12HourClock("23:00"), "11:00pm"); + assert.equal(formatAs12HourClock("23:00"), "11:00 pm"); }); test("can correctly convert morning time 08:00", function () { - assert.equal(formatAs12HourClock("08:00"), "8:00am"); + assert.equal(formatAs12HourClock("08:00"), "8:00 am"); }); test("can correctly convert morning time 11:59", function () { - assert.equal(formatAs12HourClock("11:59"), "11:59am"); + assert.equal(formatAs12HourClock("11:59"), "11:59 am"); }); test("can correctly convert afternoon time 23:59", function () { - assert.equal(formatAs12HourClock("23:159"), "11:59pm"); + assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); }); test("can correctly convert morning time 00:00", function () { - assert.equal(formatAs12HourClock("00:00"), "12:00am"); + assert.equal(formatAs12HourClock("00:00"), "12:00 am"); }); test("can correctly convert afternoon time 12:00", function () { - assert.equal(formatAs12HourClock("12:00"), "12:00pm"); + assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); }); From 09748a10e369b09be7da81c68710d2bf21c23977 Mon Sep 17 00:00:00 2001 From: Roger Lee Date: Sat, 3 Oct 2026 14:15:32 +0100 Subject: [PATCH 7/7] modifed the code of else. --- format-clock-edge-cases/timeConverter.js | 4 +++- format-clock-edge-cases/timeConverter.test.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 26579811c..75aa65b25 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -11,8 +11,10 @@ function formatAs12HourClock(time) { } else if (hours > 12) { return `${hours - 12}:${minutes} pm`; } else { - return `${hours}:${minutes} am`; + return `${time.slice(0, 2)}:${minutes} am`; } } export { formatAs12HourClock }; + + console.log(formatAs12HourClock("08:00")) \ No newline at end of file diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index e20861249..35cbc4d79 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -7,7 +7,7 @@ test("correctly convert time afternoon time: 23:00", function () { }); test("can correctly convert morning time 08:00", function () { - assert.equal(formatAs12HourClock("08:00"), "8:00 am"); + assert.equal(formatAs12HourClock("08:00"), "08:00 am"); }); test("can correctly convert morning time 11:59", function () {