From 428849e4890a0908b1c69a18aa4740f60478ae5a Mon Sep 17 00:00:00 2001 From: Abdennour Hachemi Date: Mon, 5 Oct 2026 12:00:09 +0100 Subject: [PATCH 1/9] solving time convert exercsie --- format-clock-edge-cases/timeConverter.js | 74 ++++++++++++++++++- format-clock-edge-cases/timeConverter.test.js | 46 ++++++++++-- 2 files changed, 111 insertions(+), 9 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9..6759c49dc 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,11 +1,77 @@ +// function formatAs12HourClock(time) { +// //extract digits representing hours +// const hours = Number(time.slice(0, 2)); +// //if houre value over 12, subtract 12 +// //if hour value under 12 , continue +// if (time === "00:00") { +// return `12:00 am`; +// } +// if (hours > 12) { +// // add pm and reurn value +// return `${hours - 12}:00 pm`; +// } +// //add am or pm +// //return new value +// return `${time} am`; +// } + function formatAs12HourClock(time) { + console.log(time.length) + + if (time.length == 4) { + console.log("got here") + time=time.padStart(5, "0") + } + + if (typeof time !== "string") { + return "nccorect timing format"; + } + + const match = time.match(/^(\d{1,2}):(\d{2})$/); + + if (!match) { + return "Inccorect timing format"; + } + + + const hours = Number(time.slice(0, 2)); + const minutes = Number(time.slice(3, 5)); + console.log(hours + ` <------------look here hour`); + console.log(time + ` <------------look here time`); + + + if (time === "00:00") { + return `12:00 am`; + } + if (time === "12:00") { + return `12:00 pm`; + } + - if (hours > 12) { - return `${hours - 12}:00 pm`; + if (hours < 12) { + return `${time} am`; + } else if (hours == 12) { + return `${time} am`; + } else if (hours == 24) { + return `12:00 am`; + } + + if (hours > 12 && hours < 22 && minutes < 10) { + return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`; + } + if (hours > 12 && hours < 22 && minutes >= 10) { + return `${(hours - 12).toString().padStart(2, "0")}:${minutes} pm`; + } + if (hours >= 22 && minutes < 10) { + return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`; + } + if (hours >= 22 && minutes >= 10) { + return `${hours - 12}:${minutes} 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..c8872d1f3 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -1,11 +1,47 @@ -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 corectly convert midnight", () => + assert.equal(formatAs12HourClock("00:00"), "12:00 am")); + +test("can corectly convert mid-day", () => + assert.equal(formatAs12HourClock("12:00"), "12:00 pm")); + +test("can corectly convert half an hour passed midnight", () => + assert.equal(formatAs12HourClock("00:30"), "00:30 am")); + +test("can correctly convert time with missing leading zero", function () { + assert.equal(formatAs12HourClock("9:00"), "09:00 am"); +}); + +test("can correctly handel non digit format entries", function () { + assert.equal(formatAs12HourClock("nineOclock"), "Inccorect timing format"); +}); +/* ************************ Edge cases Minutes *****************************************************/ +/* over one hour <- 60 30 0 -> negative number */ +/* <----------------|---------------------------|---------------------------------------|-------------------> */ +/* **************************************************************************************************************/ + +/* ************************ Edge cases Hours *****************************************************/ +/* over one day <- 24 12 0 -> negative number */ +/* <----------------|---------------------------|---------------------------------------|-------------------> */ +/* **************************************************************************************************************/ + +/*--------------------------------------------| + | Hours | Minures | + | 00 | 00 | + |___________________|___________________ ___| + + + * + + */ From d81fa178c937a48232e712919a340f1476ad444b Mon Sep 17 00:00:00 2001 From: Abdennour Hachemi Date: Mon, 5 Oct 2026 13:01:42 +0100 Subject: [PATCH 2/9] remove comment --- format-clock-edge-cases/timeConverter.js | 48 ++++++------------------ 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 6759c49dc..fbbd907de 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,46 +1,23 @@ -// function formatAs12HourClock(time) { -// //extract digits representing hours -// const hours = Number(time.slice(0, 2)); -// //if houre value over 12, subtract 12 -// //if hour value under 12 , continue -// if (time === "00:00") { -// return `12:00 am`; -// } -// if (hours > 12) { -// // add pm and reurn value -// return `${hours - 12}:00 pm`; -// } -// //add am or pm -// //return new value -// return `${time} am`; -// } - function formatAs12HourClock(time) { + console.log(time.length); - console.log(time.length) - if (time.length == 4) { - console.log("got here") - time=time.padStart(5, "0") + console.log("got here"); + time = time.padStart(5, "0"); + } + + if (typeof time !== "string") { + return "nccorect timing format"; } - - if (typeof time !== "string") { - return "nccorect timing format"; - } - const match = time.match(/^(\d{1,2}):(\d{2})$/); + const match = time.match(/^(\d{1,2}):(\d{2})$/); - if (!match) { - return "Inccorect timing format"; - } + if (!match) { + return "Inccorect timing format"; + } - - const hours = Number(time.slice(0, 2)); const minutes = Number(time.slice(3, 5)); - console.log(hours + ` <------------look here hour`); - console.log(time + ` <------------look here time`); - if (time === "00:00") { return `12:00 am`; @@ -48,7 +25,6 @@ function formatAs12HourClock(time) { if (time === "12:00") { return `12:00 pm`; } - if (hours < 12) { return `${time} am`; @@ -73,5 +49,3 @@ function formatAs12HourClock(time) { } export { formatAs12HourClock }; - - From 556f28385bf746439a00aad68a22cf1daf097537 Mon Sep 17 00:00:00 2001 From: Abdennour Hachemi Date: Mon, 5 Oct 2026 13:03:07 +0100 Subject: [PATCH 3/9] update code --- format-clock-edge-cases/timeConverter.test.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index c8872d1f3..216567c5c 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -41,7 +41,4 @@ test("can correctly handel non digit format entries", function () { | 00 | 00 | |___________________|___________________ ___| - - * - */ From 3e6d6c84d08c45600da7305b8d42cd9a56df89c2 Mon Sep 17 00:00:00 2001 From: Abdennour Hachemi Date: Mon, 5 Oct 2026 18:30:15 +0100 Subject: [PATCH 4/9] fixing errors --- format-clock-edge-cases/timeConverter.js | 22 +++++++++++-------- format-clock-edge-cases/timeConverter.test.js | 2 +- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index fbbd907de..f75eedb9f 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,8 +1,6 @@ function formatAs12HourClock(time) { - console.log(time.length); - if (time.length == 4) { - console.log("got here"); + c; time = time.padStart(5, "0"); } @@ -19,21 +17,24 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); const minutes = Number(time.slice(3, 5)); - if (time === "00:00") { - return `12:00 am`; - } if (time === "12:00") { return `12:00 pm`; } if (hours < 12) { return `${time} am`; - } else if (hours == 12) { - return `${time} am`; - } else if (hours == 24) { + } + if (hours >= 12) { + return `${time} pm`; + } + if (hours == 24) { return `12:00 am`; } + if (hours > 12 && hours < 22 && minutes < 10) { + return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`; + } + if (hours > 12 && hours < 22 && minutes < 10) { return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`; } @@ -46,6 +47,9 @@ function formatAs12HourClock(time) { if (hours >= 22 && minutes >= 10) { return `${hours - 12}:${minutes} pm`; } + if (hours >= 12 && minutes >= 10) { + return `${hours}:${minutes} pm`; + } } export { formatAs12HourClock }; diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 216567c5c..22021b2b4 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -17,7 +17,7 @@ test("can corectly convert mid-day", () => assert.equal(formatAs12HourClock("12:00"), "12:00 pm")); test("can corectly convert half an hour passed midnight", () => - assert.equal(formatAs12HourClock("00:30"), "00:30 am")); + assert.equal(formatAs12HourClock("00:30"), "12:30 am")); test("can correctly convert time with missing leading zero", function () { assert.equal(formatAs12HourClock("9:00"), "09:00 am"); From fc0d336b6c1a7211dda0ee8406b841c7c79c0e68 Mon Sep 17 00:00:00 2001 From: Abdennour Hachemi Date: Tue, 6 Oct 2026 15:16:38 +0100 Subject: [PATCH 5/9] correcting Errors and reorgnising code --- format-clock-edge-cases/timeConverter.js | 99 +++++++++++-------- format-clock-edge-cases/timeConverter.test.js | 2 +- 2 files changed, 58 insertions(+), 43 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index f75eedb9f..65a74e2a8 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,55 +1,70 @@ function formatAs12HourClock(time) { - if (time.length == 4) { - c; - time = time.padStart(5, "0"); - } + if (typeof time !== "string") { + return "Incorrect timing format"; + } + const correctTimingFormat = time.match(/^(\d{1,2}):(\d{2})$/); - if (typeof time !== "string") { - return "nccorect timing format"; + if (!correctTimingFormat) { + return "Incorrect timing format"; } - const match = time.match(/^(\d{1,2}):(\d{2})$/); + if (time.length == 4) { + time = time.padStart(5, "0"); + } - if (!match) { - return "Inccorect timing format"; - } + const match = time.match(/^(\d{1,2}):(\d{2})$/); - const hours = Number(time.slice(0, 2)); - const minutes = Number(time.slice(3, 5)); + if (!match) { + return "Incorrect timing format"; + } - if (time === "12:00") { - return `12:00 pm`; - } + const hours = Number(time.slice(0, 2)); + const minutes = Number(time.slice(3, 5)); - if (hours < 12) { - return `${time} am`; - } - if (hours >= 12) { - return `${time} pm`; - } - if (hours == 24) { - return `12:00 am`; - } + // 00:00 + if (time === "00:00") { + return `12:00 am`; + } - if (hours > 12 && hours < 22 && minutes < 10) { - return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`; - } + // 12:00 + if (time === "12:00") { + return `12:00 pm`; + } - if (hours > 12 && hours < 22 && minutes < 10) { - return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`; - } - if (hours > 12 && hours < 22 && minutes >= 10) { - return `${(hours - 12).toString().padStart(2, "0")}:${minutes} pm`; - } - if (hours >= 22 && minutes < 10) { - return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`; - } - if (hours >= 22 && minutes >= 10) { - return `${hours - 12}:${minutes} pm`; - } - if (hours >= 12 && minutes >= 10) { - return `${hours}:${minutes} pm`; - } + // 00:xx + if (hours === 0) { + return `12:${minutes.toString().padStart(2, "0")} am`; + } + + // 01:xx - 11:xx + if (hours < 12) { + return `${time} am`; + } + + // 12:xx + if (hours === 12) { + return `${time} pm`; + } + + // 13:00 - 21:59 + if (hours > 12 && hours < 22 && minutes < 10) { + return `${(hours - 12).toString().padStart(2, "0")}:${minutes + .toString() + .padStart(2, "0")} pm`; + } + + if (hours > 12 && hours < 22 && minutes >= 10) { + return `${(hours - 12).toString().padStart(2, "0")}:${minutes} pm`; + } + + // 22:00 - 23:59 + if (hours >= 22 && minutes < 10) { + return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`; + } + + if (hours >= 22 && minutes >= 10) { + return `${hours - 12}:${minutes} pm`; + } } export { formatAs12HourClock }; diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 22021b2b4..8315f2689 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -24,7 +24,7 @@ test("can correctly convert time with missing leading zero", function () { }); test("can correctly handel non digit format entries", function () { - assert.equal(formatAs12HourClock("nineOclock"), "Inccorect timing format"); + assert.equal(formatAs12HourClock("nineOclock"), "Incorrect timing format"); }); /* ************************ Edge cases Minutes *****************************************************/ /* over one hour <- 60 30 0 -> negative number */ From c97324b9456f64ce07dd0c1ea49c347c2b32ca7e Mon Sep 17 00:00:00 2001 From: Abdennour Hachemi Date: Tue, 6 Oct 2026 21:44:10 +0100 Subject: [PATCH 6/9] add more test cases --- format-clock-edge-cases/timeConverter.test.js | 37 ++++++++----------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 8315f2689..1285cf351 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -5,40 +5,33 @@ import test from "node:test"; 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 corectly convert midnight", () => assert.equal(formatAs12HourClock("00:00"), "12:00 am")); - test("can corectly convert mid-day", () => assert.equal(formatAs12HourClock("12:00"), "12:00 pm")); - test("can corectly convert half an hour passed midnight", () => assert.equal(formatAs12HourClock("00:30"), "12:30 am")); - test("can correctly convert time with missing leading zero", function () { assert.equal(formatAs12HourClock("9:00"), "09:00 am"); }); - test("can correctly handel non digit format entries", function () { assert.equal(formatAs12HourClock("nineOclock"), "Incorrect timing format"); }); -/* ************************ Edge cases Minutes *****************************************************/ -/* over one hour <- 60 30 0 -> negative number */ -/* <----------------|---------------------------|---------------------------------------|-------------------> */ -/* **************************************************************************************************************/ - -/* ************************ Edge cases Hours *****************************************************/ -/* over one day <- 24 12 0 -> negative number */ -/* <----------------|---------------------------|---------------------------------------|-------------------> */ -/* **************************************************************************************************************/ - -/*--------------------------------------------| - | Hours | Minures | - | 00 | 00 | - |___________________|___________________ ___| - - */ +test("can correctly handel one minute after midnight", function () { + assert.equal(formatAs12HourClock("00:01"), "12:01 am"); +}); +test("can correctly handel one minute before miday", function () { + assert.equal(formatAs12HourClock("11:59"), "11:59 am"); +}); +test("can correctly handel one minute after miday", function () { + assert.equal(formatAs12HourClock("00:01"), "12:01 am"); +}); +test("can correctly handel 1 pm", function () { + assert.equal(formatAs12HourClock("13:00"), "01:00 pm"); +}); +test("can correctly handel one minute before midnight", function () { + assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); +}); From eb2a84703725d4b84c7a5dc9c440f0b028954de0 Mon Sep 17 00:00:00 2001 From: Abdennour Hachemi Date: Tue, 6 Oct 2026 21:46:39 +0100 Subject: [PATCH 7/9] correct indentation --- format-clock-edge-cases/timeConverter.js | 96 ++++++++++++------------ 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 65a74e2a8..69d225960 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,70 +1,70 @@ function formatAs12HourClock(time) { - if (typeof time !== "string") { - return "Incorrect timing format"; - } + if (typeof time !== "string") { + return "Incorrect timing format"; + } const correctTimingFormat = time.match(/^(\d{1,2}):(\d{2})$/); if (!correctTimingFormat) { return "Incorrect timing format"; } - if (time.length == 4) { - time = time.padStart(5, "0"); - } + if (time.length == 4) { + time = time.padStart(5, "0"); + } - const match = time.match(/^(\d{1,2}):(\d{2})$/); + const match = time.match(/^(\d{1,2}):(\d{2})$/); - if (!match) { - return "Incorrect timing format"; - } + if (!match) { + return "Incorrect timing format"; + } - const hours = Number(time.slice(0, 2)); - const minutes = Number(time.slice(3, 5)); + const hours = Number(time.slice(0, 2)); + const minutes = Number(time.slice(3, 5)); - // 00:00 - if (time === "00:00") { - return `12:00 am`; - } + // 00:00 + if (time === "00:00") { + return `12:00 am`; + } - // 12:00 - if (time === "12:00") { - return `12:00 pm`; - } + // 12:00 + if (time === "12:00") { + return `12:00 pm`; + } - // 00:xx - if (hours === 0) { - return `12:${minutes.toString().padStart(2, "0")} am`; - } + // 00:xx + if (hours === 0) { + return `12:${minutes.toString().padStart(2, "0")} am`; + } - // 01:xx - 11:xx - if (hours < 12) { - return `${time} am`; - } + // 01:xx - 11:xx + if (hours < 12) { + return `${time} am`; + } - // 12:xx - if (hours === 12) { - return `${time} pm`; - } + // 12:xx + if (hours === 12) { + return `${time} pm`; + } - // 13:00 - 21:59 - if (hours > 12 && hours < 22 && minutes < 10) { - return `${(hours - 12).toString().padStart(2, "0")}:${minutes - .toString() - .padStart(2, "0")} pm`; - } + // 13:00 - 21:59 + if (hours > 12 && hours < 22 && minutes < 10) { + return `${(hours - 12).toString().padStart(2, "0")}:${minutes + .toString() + .padStart(2, "0")} pm`; + } - if (hours > 12 && hours < 22 && minutes >= 10) { - return `${(hours - 12).toString().padStart(2, "0")}:${minutes} pm`; - } + if (hours > 12 && hours < 22 && minutes >= 10) { + return `${(hours - 12).toString().padStart(2, "0")}:${minutes} pm`; + } - // 22:00 - 23:59 - if (hours >= 22 && minutes < 10) { - return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`; - } + // 22:00 - 23:59 + if (hours >= 22 && minutes < 10) { + return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`; + } - if (hours >= 22 && minutes >= 10) { - return `${hours - 12}:${minutes} pm`; - } + if (hours >= 22 && minutes >= 10) { + return `${hours - 12}:${minutes} pm`; + } } export { formatAs12HourClock }; From 738f3b7c23cdda79b2a287a6eff78a45e0e9e93e Mon Sep 17 00:00:00 2001 From: Abdennour Hachemi Date: Wed, 7 Oct 2026 09:59:43 +0100 Subject: [PATCH 8/9] remove duplicate test case --- format-clock-edge-cases/timeConverter.test.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 1285cf351..a80a954b7 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -23,12 +23,10 @@ test("can correctly handel non digit format entries", function () { test("can correctly handel one minute after midnight", function () { assert.equal(formatAs12HourClock("00:01"), "12:01 am"); }); -test("can correctly handel one minute before miday", function () { +test("can correctly handel one minute before midday", function () { assert.equal(formatAs12HourClock("11:59"), "11:59 am"); }); -test("can correctly handel one minute after miday", function () { - assert.equal(formatAs12HourClock("00:01"), "12:01 am"); -}); + test("can correctly handel 1 pm", function () { assert.equal(formatAs12HourClock("13:00"), "01:00 pm"); }); From dfa00becd562bc4001208d09e1ca5e9ce829fdcb Mon Sep 17 00:00:00 2001 From: Abdennour Hachemi Date: Wed, 7 Oct 2026 11:13:30 +0100 Subject: [PATCH 9/9] re-instating test for one minute after midday --- format-clock-edge-cases/timeConverter.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index a80a954b7..08f4be8cd 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -33,3 +33,6 @@ test("can correctly handel 1 pm", function () { test("can correctly handel one minute before midnight", function () { assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); }); +test("can correctly handle one minute after midday", function () { + assert.equal(formatAs12HourClock("12:01"), "12:01 pm"); +});