From f8a6c20f98feca8dac1b8957b123b86740a5448e Mon Sep 17 00:00:00 2001 From: Carol Date: Tue, 6 Oct 2026 23:02:02 +0100 Subject: [PATCH 1/2] 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"); }); From e6a462ca3df6aad8a8f09d5aefd1d1e036060304 Mon Sep 17 00:00:00 2001 From: Carol Date: Sat, 10 Oct 2026 08:54:29 +0100 Subject: [PATCH 2/2] updated files --- format-clock-edge-cases/timeConverter.js | 14 ++++---------- format-clock-edge-cases/timeConverter.test.js | 14 +++++++++++++- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 7696807f1..862f64a35 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,11 +1,11 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); - const minutes = time.slice(-2); + const minutes = time.slice(3, 5); - if (time == "00") { - return `12:00:${minutes} am`; + if (hours == "00") { + return `12:${minutes} am`; } else if (hours > 12) { - return `${hours - 12}:${minutes} pm`; + return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`; } else if (hours === 12) { return `12:${minutes} pm`; } else { @@ -13,10 +13,4 @@ function formatAs12HourClock(time) { } } -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 de98c8ab9..4209e09dc 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -27,7 +27,7 @@ test("can correctly convert 30 minutes before 12:00", function () { }); test("can correctly convert time at the beginning of noon", function () { - assert.equal(formatAs12HourClock("12:001"), "12:01 pm"); + assert.equal(formatAs12HourClock("12:01"), "12:01 pm"); }); test("can correctly convert half hour before midnight", function () { @@ -37,3 +37,15 @@ test("can correctly convert half hour before midnight", function () { test("can correctly convert 1 minute before midnight", function () { assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); }); + +test("can correctly convert half past midnight", function () { + assert.equal(formatAs12HourClock("00:30"), "12:30 am"); +}); + +test("can correctly convert 1 minute before midday", function () { + assert.equal(formatAs12HourClock("11:59"), "11:59 am"); +}); + +test("can correctly convert 1 hour after midday", function () { + assert.equal(formatAs12HourClock("13:00"), "01:00 pm"); +}); \ No newline at end of file