From f8a6c20f98feca8dac1b8957b123b86740a5448e Mon Sep 17 00:00:00 2001 From: Carol Date: Tue, 6 Oct 2026 23:02:02 +0100 Subject: [PATCH] updated the 2 files --- format-clock-edge-cases/timeConverter.js | 21 +++++++--- format-clock-edge-cases/timeConverter.test.js | 38 ++++++++++++++++--- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9..7696807f1 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,11 +1,22 @@ function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); + const minutes = time.slice(-2); - if (hours > 12) { - return `${hours - 12}:00 pm`; + if (time == "00") { + return `12:00:${minutes} am`; + } else if (hours > 12) { + return `${hours - 12}:${minutes} pm`; + } else if (hours === 12) { + return `12:${minutes} pm`; + } else { + return `${time} am`; } - return `${time} am`; } -export {formatAs12HourClock}; +console.log(formatAs12HourClock("23:00")); +console.log(formatAs12HourClock("14:00")); +console.log(formatAs12HourClock("12:00")); +console.log(formatAs12HourClock("08:15")); +console.log(formatAs12HourClock("00:00")); + +export { formatAs12HourClock }; diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 88f2af094..de98c8ab9 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -1,11 +1,39 @@ -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"); +}); + +test("can correctly convert midday", function () { + assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); +}); + +test("can correctly convert midnight", function () { + assert.equal(formatAs12HourClock("00:00"), "12:00 am"); +}); + +test("can correctly convert 2 hours before 12:00", function () { + assert.equal(formatAs12HourClock("10:00"), "10:00 am"); +}); + +test("can correctly convert 30 minutes before 12:00", function () { + assert.equal(formatAs12HourClock("11:30"), "11:30 am"); +}); + +test("can correctly convert time at the beginning of noon", function () { + assert.equal(formatAs12HourClock("12:001"), "12:01 pm"); +}); + +test("can correctly convert half hour before midnight", function () { + assert.equal(formatAs12HourClock("23:30"), "11:30 pm"); +}); + +test("can correctly convert 1 minute before midnight", function () { + assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); });