From 6e75e0cc2c97e9b61d8005743e672a9fcb3626e1 Mon Sep 17 00:00:00 2001 From: BrianY315 Date: Tue, 6 Oct 2026 11:55:28 +0100 Subject: [PATCH 1/4] Fix 12-hour clock edge cases Cover valid time boundaries and preserve minutes across conversion. --- format-clock-edge-cases/timeConverter.js | 13 +++++++-- format-clock-edge-cases/timeConverter.test.js | 28 +++++++++++++------ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9..b1aa1b059 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,10 +1,19 @@ function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3); + + if (hours === 0) { + return `12:${minutes} am`; + } if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${hours - 12}:${minutes} pm`; } + + if (hours === 12) { + return `${time} pm`; + } + return `${time} am`; } diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 88f2af094..293fff503 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -1,11 +1,23 @@ -import {formatAs12HourClock} from "./timeConverter.js"; -import assert from "node:assert"; +import { formatAs12HourClock } from "./timeConverter.js"; +import assert from "node:assert/strict"; import test from "node:test"; -test("correctly convert time after 12:00", function(){ - assert.equal(formatAs12HourClock("23:00"), "11:00 pm"); -}); +const cases = [ + ["00:00", "12:00 am"], + ["00:01", "12:01 am"], + ["01:05", "01:05 am"], + ["08:00", "08:00 am"], + ["11:59", "11:59 am"], + ["12:00", "12:00 pm"], + ["12:34", "12:34 pm"], + ["13:00", "1:00 pm"], + ["15:45", "3:45 pm"], + ["23:00", "11:00 pm"], + ["23:59", "11:59 pm"], +]; -test("can correctly convert morning time", function() { - assert.equal(formatAs12HourClock("08:00"), "08:00 am"); -}); +for (const [time, expected] of cases) { + test(`${time} converts to ${expected}`, () => { + assert.equal(formatAs12HourClock(time), expected); + }); +} From 153637b93e359db8c4cf26142e6ebaabbaa5485f Mon Sep 17 00:00:00 2001 From: BrianY315 Date: Tue, 6 Oct 2026 12:00:15 +0100 Subject: [PATCH 2/4] Match approved clock formatting cases Pad single-digit afternoon hours and align edge-case test formatting. --- format-clock-edge-cases/timeConverter.js | 3 ++- format-clock-edge-cases/timeConverter.test.js | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index b1aa1b059..c1ce46be4 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -7,7 +7,8 @@ function formatAs12HourClock(time) { } if (hours > 12) { - return `${hours - 12}:${minutes} pm`; + const pmHours = String(hours - 12).padStart(2, "0"); + return `${pmHours}:${minutes} pm`; } if (hours === 12) { diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 293fff503..61a172bcb 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -10,8 +10,9 @@ const cases = [ ["11:59", "11:59 am"], ["12:00", "12:00 pm"], ["12:34", "12:34 pm"], - ["13:00", "1:00 pm"], - ["15:45", "3:45 pm"], + ["13:00", "01:00 pm"], + ["13:30", "01:30 pm"], + ["15:45", "03:45 pm"], ["23:00", "11:00 pm"], ["23:59", "11:59 pm"], ]; From ebef7eb7332fb164b6f4483bb251e1426edfe2b7 Mon Sep 17 00:00:00 2001 From: BrianY315 Date: Tue, 6 Oct 2026 12:04:57 +0100 Subject: [PATCH 3/4] Test every valid clock input Exercise all 1,440 valid HH:mm values against the 12-hour output. --- format-clock-edge-cases/timeConverter.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 61a172bcb..50bc9b53b 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -22,3 +22,16 @@ for (const [time, expected] of cases) { assert.equal(formatAs12HourClock(time), expected); }); } + +test("converts every valid minute of the day", () => { + for (let hour = 0; hour < 24; hour += 1) { + for (let minute = 0; minute < 60; minute += 1) { + const time = `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`; + const convertedHour = hour % 12 || 12; + const period = hour < 12 ? "am" : "pm"; + const expected = `${String(convertedHour).padStart(2, "0")}:${String(minute).padStart(2, "0")} ${period}`; + + assert.equal(formatAs12HourClock(time), expected, time); + } + } +}); From d38ce3a48b34eee698580823e1b71466de9bacb5 Mon Sep 17 00:00:00 2001 From: BrianY315 Date: Wed, 7 Oct 2026 18:00:05 +0100 Subject: [PATCH 4/4] Guard formatAs12HourClock against non-string input --- format-clock-edge-cases/timeConverter.js | 4 ++++ format-clock-edge-cases/timeConverter.test.js | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index c1ce46be4..740d3c093 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,4 +1,8 @@ function formatAs12HourClock(time) { + if (typeof time !== "string") { + throw new TypeError("time must be a string in HH:mm format"); + } + const hours = Number(time.slice(0, 2)); const minutes = time.slice(3); diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 50bc9b53b..28199301d 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -23,6 +23,12 @@ for (const [time, expected] of cases) { }); } +for (const value of [true, false, [], ["08:00"], {}, 800, null, undefined]) { + test(`throws a TypeError for non-string input ${JSON.stringify(value)}`, () => { + assert.throws(() => formatAs12HourClock(value), TypeError); + }); +} + test("converts every valid minute of the day", () => { for (let hour = 0; hour < 24; hour += 1) { for (let minute = 0; minute < 60; minute += 1) {