From 92fc81cb7cb04084f6a2839ec127f97a2f77c716 Mon Sep 17 00:00:00 2001 From: cyf Date: Thu, 1 Oct 2026 22:50:21 +0100 Subject: [PATCH 1/6] format code for midnight(00:00) --- format-clock-edge-cases/timeConverter.js | 9 +++++---- format-clock-edge-cases/timeConverter.test.js | 14 +++++++++----- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9..9f0ddc0f9 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,11 +1,12 @@ function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); - + if (hours === 0) { + return `${hours + 12}${time.slice(2, 5)} am`; + } if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${hours - 12}${time.slice(2, 5)} pm`; } return `${time} am`; } -export {formatAs12HourClock}; +export { formatAs12HourClock }; diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 88f2af094..a203a76a7 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -1,11 +1,15 @@ -import {formatAs12HourClock} from "./timeConverter.js"; +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("correctly convert time after 12:00", function () { + assert.equal(formatAs12HourClock("23:00"), "11:00 pm"); }); -test("can correctly convert morning time", function() { - assert.equal(formatAs12HourClock("08:00"), "08:00 am"); +test("can correctly convert morning time", function () { + assert.equal(formatAs12HourClock("08:00"), "08:00 am"); +}); + +test("can correctly convert midnight time", function () { + assert.equal(formatAs12HourClock("00:00"), "12:00 am"); }); From 710770bcea065d88b698eb364317d922e1aa5e09 Mon Sep 17 00:00:00 2001 From: cyf Date: Thu, 1 Oct 2026 23:18:20 +0100 Subject: [PATCH 2/6] Fix 12-hour clock conversion edge cases --- format-clock-edge-cases/timeConverter.js | 7 +++++-- format-clock-edge-cases/timeConverter.test.js | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 9f0ddc0f9..d3e5a43ae 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,10 +1,13 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); if (hours === 0) { - return `${hours + 12}${time.slice(2, 5)} am`; + return `${String(hours + 12).padStart(2, "0")}${time.slice(2, 5)} am`; + } + if (hours === 12) { + return `${time} pm`; } if (hours > 12) { - return `${hours - 12}${time.slice(2, 5)} pm`; + return `${String(hours - 12).padStart(2, "0")}${time.slice(2, 5)} pm`; } return `${time} am`; } diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index a203a76a7..2a5ee65be 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -13,3 +13,11 @@ test("can correctly convert morning time", function () { test("can correctly convert midnight time", function () { assert.equal(formatAs12HourClock("00:00"), "12:00 am"); }); + +test("can correctly convert minutes", function () { + assert.equal(formatAs12HourClock("21:08"), "09:08 pm"); +}); + +test("can correctly convert noon", function () { + assert.equal(formatAs12HourClock("12:30"), "12:30 pm"); +}); From d5ea319818c241409cb718bb1aa6033dd5060fdb Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 1 Oct 2026 23:32:48 +0100 Subject: [PATCH 3/6] add some more test for check --- format-clock-edge-cases/timeConverter.test.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 2a5ee65be..e6c4d289c 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -19,5 +19,11 @@ test("can correctly convert minutes", function () { }); test("can correctly convert noon", function () { - assert.equal(formatAs12HourClock("12:30"), "12:30 pm"); + assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); +}); +test("can correctly convert noon", function () { + assert.equal(formatAs12HourClock("11:59"), "11:59 am"); +}); +test("can correctly convert one hour after noon", function () { + assert.equal(formatAs12HourClock("13:00"), "01:00 pm"); }); From 255f29da0a5950ce17c069aece9130c2d295931e Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Mon, 5 Oct 2026 10:34:44 +0100 Subject: [PATCH 4/6] cover all 6 hour cases and both single-digit and double-digit minute cases --- format-clock-edge-cases/timeConverter.test.js | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index e6c4d289c..d3c32989a 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -2,6 +2,30 @@ import { formatAs12HourClock } from "./timeConverter.js"; import assert from "node:assert"; import test from "node:test"; +test("can correctly convert midnight with double-digit minutes", function () { + assert.equal(formatAs12HourClock("00:15"), "12:15 am"); +}); + +test("can correctly convert noon with double-digit minutes", function () { + assert.equal(formatAs12HourClock("12:15"), "12:15 pm"); +}); + +test("can correctly convert morning single-digit hours with double-digit minutes", function () { + assert.equal(formatAs12HourClock("08:15"), "08:15 am"); +}); + +test("can correctly convert late morning with single-digit minutes", function () { + assert.equal(formatAs12HourClock("11:05"), "11:05 am"); +}); + +test("can correctly convert afternoon with double-digit minutes", function () { + assert.equal(formatAs12HourClock("13:15"), "01:15 pm"); +}); + +test("can correctly convert late evening with single-digit minutes", function () { + assert.equal(formatAs12HourClock("23:05"), "11:05 pm"); +}); + test("correctly convert time after 12:00", function () { assert.equal(formatAs12HourClock("23:00"), "11:00 pm"); }); @@ -21,7 +45,7 @@ test("can correctly convert minutes", function () { test("can correctly convert noon", function () { assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); }); -test("can correctly convert noon", function () { +test("can correctly convert one minute before noon", function () { assert.equal(formatAs12HourClock("11:59"), "11:59 am"); }); test("can correctly convert one hour after noon", function () { From 7eaaddab15ea8cc6474709361323f7c1c168d1c3 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Mon, 5 Oct 2026 11:41:06 +0100 Subject: [PATCH 5/6] add minute variable --- format-clock-edge-cases/timeConverter.js | 5 +++-- format-clock-edge-cases/timeConverter.test.js | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index d3e5a43ae..6d71e151d 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,13 +1,14 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(-3); if (hours === 0) { - return `${String(hours + 12).padStart(2, "0")}${time.slice(2, 5)} am`; + return `12${minutes} am`; } if (hours === 12) { return `${time} pm`; } if (hours > 12) { - return `${String(hours - 12).padStart(2, "0")}${time.slice(2, 5)} pm`; + return `${String(hours - 12).padStart(2, "0")}${minutes} pm`; } return `${time} am`; } diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index d3c32989a..33e06c9f2 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -22,6 +22,14 @@ test("can correctly convert afternoon with double-digit minutes", function () { assert.equal(formatAs12HourClock("13:15"), "01:15 pm"); }); +test("can correctly convert afternoon with double-digit minutes", function () { + assert.equal(formatAs12HourClock("13:15"), "01:15 pm"); +}); + +test("can correctly convert afternoon with double-digit minutes", function () { + assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); +}); + test("can correctly convert late evening with single-digit minutes", function () { assert.equal(formatAs12HourClock("23:05"), "11:05 pm"); }); From c62705ce3e903e05b3074576cc3b29c09051558c Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Mon, 5 Oct 2026 11:45:48 +0100 Subject: [PATCH 6/6] change test comment --- format-clock-edge-cases/timeConverter.test.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 33e06c9f2..895187216 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -22,15 +22,11 @@ test("can correctly convert afternoon with double-digit minutes", function () { assert.equal(formatAs12HourClock("13:15"), "01:15 pm"); }); -test("can correctly convert afternoon with double-digit minutes", function () { - assert.equal(formatAs12HourClock("13:15"), "01:15 pm"); -}); - -test("can correctly convert afternoon with double-digit minutes", function () { +test("can correctly convert late evening with double-digit minutes", function () { assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); }); -test("can correctly convert late evening with single-digit minutes", function () { +test("can correctly convert evening with single-digit minutes", function () { assert.equal(formatAs12HourClock("23:05"), "11:05 pm"); });