diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9..c77cf5a17 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,11 +1,15 @@ function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); + const min = Number(time.slice(-2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; + if (hours === 12) { + return `${time} pm`; + } else if (hours === 0) { + return `${(hours + 12).toString().padStart(2, "0")}:${min.toString().padStart(2, "0")} am`; + } else if (hours > 12) { + return `${(hours - 12).toString().padStart(2, "0")}:${min.toString().padStart(2, "0")} 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..916f14353 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -1,11 +1,49 @@ -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"); + assert.equal(formatAs12HourClock("01:00"), "01:00 am"); +}); + +test("can correctly convert time with minutes for am", function () { + assert.equal(formatAs12HourClock("08:35"), "08:35 am"); +}); + +test("can correctly convert time with minutes for pm", function () { + assert.equal(formatAs12HourClock("18:35"), "06:35 pm"); +}); + +test("can correctly convert time with minutes with 0 pad such as 13:04", function () { + assert.equal(formatAs12HourClock("13:04"), "01:04 pm"); +}); + +test("can correctly convert time with hour being 00 to show as 12 am", function () { + assert.equal(formatAs12HourClock("00:00"), "12:00 am"); + assert.equal(formatAs12HourClock("00:30"), "12:30 am"); +}); + +test("can correctly convert time with just after midnight to still show 12 am with minutes added", function () { + assert.equal(formatAs12HourClock("00:01"), "12:01 am"); +}); + +test("last minute before 12 pm", function () { + assert.equal(formatAs12HourClock("11:59"), "11:59 am"); +}); +test("noon is 12 pm", function () { + assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); +}); +test("first minute after noon", function () { + assert.equal(formatAs12HourClock("12:01"), "12:01 pm"); +}); +test("first hour after noon is 1 pm", function () { + assert.equal(formatAs12HourClock("13:00"), "01:00 pm"); +}); +test("last minute before midnight", function () { + assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); });