From 35054c4884e77f5d7f34962251479b23f5f8efcb Mon Sep 17 00:00:00 2001 From: Yonatan teklemariam Date: Mon, 5 Oct 2026 23:22:01 +0100 Subject: [PATCH 1/2] completed Sprint-1 exercises --- format-clock-edge-cases/timeConverter.js | 21 ++++++++++++--- format-clock-edge-cases/timeConverter.test.js | 26 ++++++++++++++----- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9..4bdf66518 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,11 +1,26 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + const paddedPMHours =(hours - 12).toString().padStart(2, "0"); + const paddedAMHours = hours.toString().padStart(2, "0"); - if (hours > 12) { - return `${hours - 12}:00 pm`; + if (hours < 0 || hours > 24 || minutes < 0 || minutes > 59) { + return `Invalid time`; } - return `${time} am`; + if (hours === 0) { + return `12:${minutes} AM`; + } + if (hours === 12) { + return `12:${minutes} PM`; + } + if (hours === 24) { + return `12:${minutes} AM`; + } + if (hours > 12) { + return `${paddedPMHours}:${minutes} PM`; + } + return `${paddedAMHours}:${minutes} AM`; } export {formatAs12HourClock}; diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 88f2af094..f7230f91e 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -1,11 +1,25 @@ -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"); -}); +const cases = [ + ["08:00", "08:00 AM"], + ["23:00", "11:00 PM"], + ["15:30", "03:30 PM"], + ["12:10", "12:10 PM"], + ["08:45", "08:45 AM"], + ["09:05", "09:05 AM"], + ["17:45", "05:45 PM"], + ["00:00", "12:00 AM"], + ["11:59", "11:59 AM"], + ["23:59", "11:59 PM"], +]; -test("can correctly convert morning time", function() { - assert.equal(formatAs12HourClock("08:00"), "08:00 am"); +cases.forEach(([input, expected]) => { + const output = formatAs12HourClock(input); + if (output === expected) { + console.log(`✅ ${input} → returned "${output}"`); + } else { + console.error(`❌ ${input} → returned "${output}", expected "${expected}"`); + } }); From 65fc201344313de1f223372b7120f5b9f2d1f3ab Mon Sep 17 00:00:00 2001 From: Yonatan teklemariam Date: Tue, 6 Oct 2026 21:48:50 +0100 Subject: [PATCH 2/2] added some corrections on the time converter test cases and the js function --- format-clock-edge-cases/timeConverter.js | 31 +++++----- format-clock-edge-cases/timeConverter.test.js | 62 +++++++++++++------ 2 files changed, 57 insertions(+), 36 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 4bdf66518..4ffa38ac8 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,26 +1,25 @@ function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); const minutes = time.slice(3, 5); - const paddedPMHours =(hours - 12).toString().padStart(2, "0"); - const paddedAMHours = hours.toString().padStart(2, "0"); + const paddedPmHours = (hours - 12).toString().padStart(2, "0"); + const paddedAmHours = hours.toString().padStart(2, "0"); if (hours < 0 || hours > 24 || minutes < 0 || minutes > 59) { return `Invalid time`; } - if (hours === 0) { - return `12:${minutes} AM`; - } - if (hours === 12) { - return `12:${minutes} PM`; - } - if (hours === 24) { - return `12:${minutes} AM`; - } - if (hours > 12) { - return `${paddedPMHours}:${minutes} PM`; + if (hours === 0) { + return `12:${minutes} am`; + } + if (hours === 12) { + return `12:${minutes} pm`; + } + if (hours === 24) { + return `12:${minutes} am`; + } + if (hours > 12) { + return `${paddedPmHours}:${minutes} pm`; } - return `${paddedAMHours}:${minutes} AM`; + return `${paddedAmHours}:${minutes} am`; } -export {formatAs12HourClock}; +export { formatAs12HourClock }; diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index f7230f91e..b65322393 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -2,24 +2,46 @@ import { formatAs12HourClock } from "./timeConverter.js"; import assert from "node:assert"; import test from "node:test"; -const cases = [ - ["08:00", "08:00 AM"], - ["23:00", "11:00 PM"], - ["15:30", "03:30 PM"], - ["12:10", "12:10 PM"], - ["08:45", "08:45 AM"], - ["09:05", "09:05 AM"], - ["17:45", "05:45 PM"], - ["00:00", "12:00 AM"], - ["11:59", "11:59 AM"], - ["23:59", "11:59 PM"], -]; - -cases.forEach(([input, expected]) => { - const output = formatAs12HourClock(input); - if (output === expected) { - console.log(`✅ ${input} → returned "${output}"`); - } else { - console.error(`❌ ${input} → returned "${output}", expected "${expected}"`); - } +test("correctly convert 08:00 to morning time", function () { + assert.equal(formatAs12HourClock("08:00"), "08:00 am"); +}); + +test("correctly convert 23:00 to evening time", function () { + assert.equal(formatAs12HourClock("23:00"), "11:00 pm"); +}); + +test("correctly convert 15:30 to afternoon time", function () { + assert.equal(formatAs12HourClock("15:30"), "03:30 pm"); +}); + +test("correctly convert 12:10 to 10 past noon time", function () { + assert.equal(formatAs12HourClock("12:10"), "12:10 pm"); +}); + +test("correctly convert 12:00 to exactly noon time", function () { + assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); +}); + +test("correctly convert 08:45 to morning time", function () { + assert.equal(formatAs12HourClock("08:45"), "08:45 am"); +}); + +test("correctly convert 09:05 to morning time", function () { + assert.equal(formatAs12HourClock("09:05"), "09:05 am"); +}); + +test("correctly convert 17:45 to evening time", function () { + assert.equal(formatAs12HourClock("17:45"), "05:45 pm"); +}); + +test("correctly convert midnight 00:00", function () { + assert.equal(formatAs12HourClock("00:00"), "12:00 am"); +}); + +test("correctly convert 11:59 to late morning time", function () { + assert.equal(formatAs12HourClock("11:59"), "11:59 am"); +}); + +test("correctly convert 23:59 to late night time", function () { + assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); });