From f54872d6e59b7f5acfa905fbdaf74fcba5d22f09 Mon Sep 17 00:00:00 2001 From: mandip sanger Date: Sat, 31 Oct 2026 16:09:30 +0000 Subject: [PATCH] edge cases --- format-clock-edge-cases/timeConverter.js | 20 +++++++++++++------ format-clock-edge-cases/timeConverter.test.js | 14 ++++++++++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9..bb18c71c2 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,11 +1,19 @@ function formatAs12HourClock(time) { + let hours = Number(time.slice(0, 2)); + const minutes = time.slice(3); - const hours = Number(time.slice(0, 2)); + const period = hours >= 12 ? "pm" : "am"; - if (hours > 12) { - return `${hours - 12}:00 pm`; - } - return `${time} am`; + if (hours === 0) { + hours = 12; + } else if (hours > 12) { + hours -= 12; + } + + const formattedHours = + hours < 10 && hours !== 12 ? `0${hours}` : hours; + + return `${formattedHours}:${minutes} ${period}`; } -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..b861f9a39 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -1,11 +1,19 @@ -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(){ +test("correctly convert time after 12:00", function () { assert.equal(formatAs12HourClock("23:00"), "11:00 pm"); }); -test("can correctly convert morning time", function() { +test("can correctly convert morning time", function () { assert.equal(formatAs12HourClock("08:00"), "08:00 am"); }); + +test("converts midnight (00:00)", function () { + assert.equal(formatAs12HourClock("00:00"), "12:00 am"); +}); + +test("converts noon (12:00)", function () { + assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); +}); \ No newline at end of file