From 18ec10812785f3f373c967094a6c376cb055ee01 Mon Sep 17 00:00:00 2001 From: Aklilu Mihtsun Date: Tue, 6 Oct 2026 18:41:32 +0100 Subject: [PATCH 1/4] added edge cases --- format-clock-edge-cases/timeConverter.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9..2908702c5 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 slicedMinutes = time.slice(-2); - if (hours > 12) { - return `${hours - 12}:00 pm`; + if (hours === 0) { + return `12:${slicedMinutes} am`; + } else if (hours > 1 && hours <= 11) { + return `0${hours}:${slicedMinutes} am`; + } else if (hours === 12) { + return `12:${slicedMinutes} pm`; + } else if (hours > 12 && hours <= 21) { + return `0${hours - 12}:${slicedMinutes} pm`; + } else if (hours === 22 || hours === 23) { + return `${hours - 12}:${slicedMinutes} pm`; } - return `${time} am`; } -export {formatAs12HourClock}; +export { formatAs12HourClock }; From 6a11e56ab5885033515899676a25e8293b33aa5d Mon Sep 17 00:00:00 2001 From: Aklilu Mihtsun Date: Tue, 6 Oct 2026 18:42:46 +0100 Subject: [PATCH 2/4] checked all the edge cases --- format-clock-edge-cases/timeConverter.test.js | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 88f2af094..d9f588a6a 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 { 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("14:00"), "02: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"); +}); + +test("can correctly convert midday time", function () { + assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); +}); + +test("can correctly convert 22:00 and 23:00 time", function () { + assert.equal(formatAs12HourClock("22:00"), "10:00 pm"); }); From 92401836f79d8f475823eeaa12b9efb0e3fe2230 Mon Sep 17 00:00:00 2001 From: Aklilu Mihtsun Date: Thu, 8 Oct 2026 00:07:50 +0100 Subject: [PATCH 3/4] improved time validation and formatting logic --- format-clock-edge-cases/timeConverter.js | 33 ++++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 2908702c5..4018e82f3 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,18 +1,29 @@ function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); + const slicedHours = time.slice(0, 2); + const hours = Number(slicedHours); const slicedMinutes = time.slice(-2); + const minutes = Number(slicedMinutes); + const pattern = /^\d{2}:\d{2}$/; - if (hours === 0) { - return `12:${slicedMinutes} am`; - } else if (hours > 1 && hours <= 11) { - return `0${hours}:${slicedMinutes} am`; - } else if (hours === 12) { - return `12:${slicedMinutes} pm`; - } else if (hours > 12 && hours <= 21) { - return `0${hours - 12}:${slicedMinutes} pm`; - } else if (hours === 22 || hours === 23) { - return `${hours - 12}:${slicedMinutes} pm`; + if (typeof time === "string" && pattern.test(time)) { + if (minutes < 60) { + if (hours < 24) { + if (hours === 0) { + return `12:${slicedMinutes} am`; + } else if (hours === 12) { + return `${hours}:${slicedMinutes} pm`; + } else if (hours > 12) { + return `${(hours - 12).toString().padStart(2, "0")}:${slicedMinutes} pm`; + } else { + return `${time} am`; + } + } + + return `Please check your HH input must be less than 24`; + } + return `Please check your MM input must be less than 60`; } + return `Please check your format input must be 'HH:MM'`; } export { formatAs12HourClock }; From fdef18296a676ec65621c335923d1c5b6c6ee3bb Mon Sep 17 00:00:00 2001 From: Aklilu Mihtsun Date: Thu, 8 Oct 2026 00:08:19 +0100 Subject: [PATCH 4/4] add tests for invalid time input handling --- format-clock-edge-cases/timeConverter.test.js | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index d9f588a6a..9515fb442 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -18,6 +18,23 @@ test("can correctly convert midday time", function () { assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); }); -test("can correctly convert 22:00 and 23:00 time", function () { - assert.equal(formatAs12HourClock("22:00"), "10:00 pm"); +test("can respond to the invalid MM input", function () { + assert.equal( + formatAs12HourClock("22:60"), + "Please check your MM input must be less than 60" + ); +}); + +test("can respond to the invalid HH input", function () { + assert.equal( + formatAs12HourClock("24:00"), + "Please check your HH input must be less than 24" + ); +}); + +test("can respond to the invalid format input", function () { + assert.equal( + formatAs12HourClock("2400"), + "Please check your format input must be 'HH:MM'" + ); });