diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9..6f8871e21 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 period = ""; + let hourString = ""; - if (hours > 12) { - return `${hours - 12}:00 pm`; + if (hours <= 11){ + period = "am"; + } else { + period = "pm"; } - return `${time} am`; + + if (hours >= 13){ + hourString = hours - 12 < 10 ? `0${hours - 12}` : `${hours -12}`; + } else if (hours === 0){ + hourString = `${hours + 12}`; + } else { + hourString = hours < 10 ? `0${hours}`:`${hours}`; + } + + return `${hourString}:${minutes} ${period}` } -export {formatAs12HourClock}; +export { formatAs12HourClock }; diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 88f2af094..6f618a722 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -1,11 +1,24 @@ -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("can format time after 12:00", () => + 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 format morning time", () => + assert.equal(formatAs12HourClock("08:00"), "08:00 am")); + +test("can format noon time", () => + assert.equal(formatAs12HourClock("12:00"), "12:00 pm")); + +test("can format afternoon time with minutes other than 00", () => + assert.equal(formatAs12HourClock("15:45"), "03:45 pm")); + +test("can format morning time with minutes other than 00", () => + assert.equal(formatAs12HourClock("08:25"), "08:25 am")); + +test("can format early noon with minutes other than 00", () => + assert.equal(formatAs12HourClock("12:17"), "12:17 pm")); + +test("can format time between midnight and 1 am", () => + assert.equal(formatAs12HourClock("00:15"), "12:15 am"));