From b98a9fb0090b6f6348053c8db12ac6960b35844f Mon Sep 17 00:00:00 2001 From: Tyler-leigh Ross Date: Mon, 5 Oct 2026 01:25:46 +0200 Subject: [PATCH 1/3] Fixed bugs and added more test cases --- format-clock-edge-cases/timeConverter.js | 26 ++++++++++++++----- format-clock-edge-cases/timeConverter.test.js | 12 +++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9..627e8208c 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,11 +1,25 @@ function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); + const minutes = time.slice(-2); + let hours12; + let timePeriod; - if (hours > 12) { - return `${hours - 12}:00 pm`; + if (hours === 0) { + hours12 = "12"; + timePeriod = "am"; + } + else if (hours === 12) { + hours12 = "12"; + timePeriod = "pm"; + } + else if (hours < 12) { + hours12 = String(hours12).padStart(2, "0"); + timePeriod = "am"; } - return `${time} am`; + else if (hours > 12) { + hours12 = String(hours12 - 12).padStart(2, "0"); + timePeriod = "pm" + } + return `${hours12}:${minutes} ${timePeriod}` } - -export {formatAs12HourClock}; +export {formatAs12HourClock}; \ 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 88f2af094..d750c5b33 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -9,3 +9,15 @@ test("correctly convert time after 12:00", function(){ test("can correctly convert morning time", function() { assert.equal(formatAs12HourClock("08:00"), "08:00 am"); }); + +test("can correctly convert midnight", function () { + assert.equal(formatAs12HourClock("00:00"), "12:00 am"); +}); + +test("can correctly convert noon", function () { + assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); +}); + +test("can correctly convert time with minutes", function () { + assert.equal(formatAs12HourClock("11:59"), "11:59 am"); +}); \ No newline at end of file From 88f744328a68239b001700c156cecf9fc22c1d73 Mon Sep 17 00:00:00 2001 From: Tyler-leigh Ross Date: Tue, 6 Oct 2026 11:10:01 +0200 Subject: [PATCH 2/3] Added more test cases with help from AI --- format-clock-edge-cases/timeConverter.test.js | 92 +++++++++++++++++-- 1 file changed, 82 insertions(+), 10 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index d750c5b33..9379df376 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -2,14 +2,7 @@ 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 correctly convert morning time", function() { - assert.equal(formatAs12HourClock("08:00"), "08:00 am"); -}); - +// Edge cases ---------------------------------------------------------------------------------------------------------------------------- test("can correctly convert midnight", function () { assert.equal(formatAs12HourClock("00:00"), "12:00 am"); }); @@ -18,6 +11,85 @@ test("can correctly convert noon", function () { assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); }); -test("can correctly convert time with minutes", function () { +// Zero-padding tests ---------------------------------------------------------------------------------------------------------------------------- +test("pads single digit hours with a leading zero", function () { + assert.equal(formatAs12HourClock("01:00"), "01:00 am"); + assert.equal(formatAs12HourClock("09:30"), "09:30 am"); +}); + +test("pads single digit minutes with a leading zero", function () { + assert.equal(formatAs12HourClock("08:05"), "08:05 am"); + assert.equal(formatAs12HourClock("00:05"), "12:05 am"); +}); + +// Noon boundary tests ---------------------------------------------------------------------------------------------------------------------------- +test("handles the minute just before noon", function () { assert.equal(formatAs12HourClock("11:59"), "11:59 am"); -}); \ No newline at end of file +}); + +test("handles the minute just after noon", function () { + assert.equal(formatAs12HourClock("12:01"), "12:01 pm"); +}); + +// Midnight boundary tests ---------------------------------------------------------------------------------------------------------------------------- +test("handles the minute just before midnight", function () { + assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); +}); + +test("handles the minute just after midnight", function () { + assert.equal(formatAs12HourClock("00:01"), "12:01 am"); +}); + +// First pm hour after noon ---------------------------------------------------------------------------------------------------------------------------- +test("converts 13:00 to 01:00 pm", function () { + assert.equal(formatAs12HourClock("13:00"), "01:00 pm"); +}); + +// 24 hour sweep tests ---------------------------------------------------------------------------------------------------------------------------------- +test("correctly converts every hour of the day", function () { + const expected = [ + "12:00 am", + "01:00 am", + "02:00 am", + "03:00 am", + "04:00 am", + "05:00 am", + "06:00 am", + "07:00 am", + "08:00 am", + "09:00 am", + "10:00 am", + "11:00 am", + "12:00 pm", + "01:00 pm", + "02:00 pm", + "03:00 pm", + "04:00 pm", + "05:00 pm", + "06:00 pm", + "07:00 pm", + "08:00 pm", + "09:00 pm", + "10:00 pm", + "11:00 pm", + ]; + + for (let hour = 0; hour < 24; hour++) { + const input = String(hour).padStart(2, "0") + ":00"; + assert.equal( + formatAs12HourClock(input), + expected[hour], + `failed for input ${input}` + ); + } +}); + +// Reason behind my test cases +// - Covers midnight and noon edge cases +// - Converts am time correctly +// - Converts pm time correctly +// - Converts time with minutes correctly +// - Verifies zero-padding on both hours and minutes +// - Verifies the exact am/pm boundary (11:59 -> 12:00 -> 12:01) +// - Verifies the exact midnight boundary (23:59 -> 00:00 -> 00:01) +// - Full 24-hour sweep to guard against future regressions \ No newline at end of file From e033237a1e21e881a9b7e1da43458274d05000c6 Mon Sep 17 00:00:00 2001 From: Tyler-leigh Ross Date: Tue, 6 Oct 2026 11:14:47 +0200 Subject: [PATCH 3/3] NaN error fixed --- format-clock-edge-cases/timeConverter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 627e8208c..a2c853e22 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -13,11 +13,11 @@ function formatAs12HourClock(time) { timePeriod = "pm"; } else if (hours < 12) { - hours12 = String(hours12).padStart(2, "0"); + hours12 = String(hours).padStart(2, "0"); timePeriod = "am"; } else if (hours > 12) { - hours12 = String(hours12 - 12).padStart(2, "0"); + hours12 = String(hours - 12).padStart(2, "0"); timePeriod = "pm" } return `${hours12}:${minutes} ${timePeriod}`