From 428849e4890a0908b1c69a18aa4740f60478ae5a Mon Sep 17 00:00:00 2001 From: Abdennour Hachemi Date: Mon, 5 Oct 2026 12:00:09 +0100 Subject: [PATCH 01/12] 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 02/12] 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 03/12] 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 04/12] 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 05/12] 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 06/12] 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 07/12] 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 08/12] 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 09/12] 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"); +}); From a4436fe9b2a02d7f79ed71f86717d4eb55351040 Mon Sep 17 00:00:00 2001 From: Abdennour Hachemi Date: Tue, 13 Oct 2026 00:58:01 +0100 Subject: [PATCH 10/12] solve count an d repeat exercises --- practice-tdd/count.js | 8 +++++++- practice-tdd/count.test.js | 11 +++++++++++ practice-tdd/repeat-str.js | 14 ++++++++++++-- practice-tdd/repeat-str.test.js | 25 +++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/practice-tdd/count.js b/practice-tdd/count.js index 5518c5712..ae98c6a43 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,3 +1,9 @@ export function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + for (const character of stringOfCharacters) { + if (character === findCharacter) { + count++; + } + } + return count; } diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index cb3aaae59..e5af59b11 100644 --- a/practice-tdd/count.test.js +++ b/practice-tdd/count.test.js @@ -1,5 +1,8 @@ + // implement a function countChar that counts the number of times a character occurs in a string import { countChar } from "./count.js"; +import { test, expect } from "@jest/globals"; + // Given a string `str` and a single character `char` to search for, // When the countChar function is called with these inputs, // Then it should: @@ -22,3 +25,11 @@ test("should count multiple occurrences of a character", () => { // And a character `char` that does not exist within `str`. // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of `char` were found. + +test("should indicate no occurrences of a character", () => { + const str = "abcde"; + const char = "f"; + const numberOfOcurrence= countChar(str, char); + expect(numberOfOcurrence).toEqual(0); +}); + diff --git a/practice-tdd/repeat-str.js b/practice-tdd/repeat-str.js index af273f3b0..4351f2e82 100644 --- a/practice-tdd/repeat-str.js +++ b/practice-tdd/repeat-str.js @@ -1,5 +1,15 @@ -export function repeatStr() { +export function repeatStr(str, count) { // Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat). // The goal is to re-implement that function, not to use it. - return "hellohellohello"; + if (count < 0) { + return "Error! negative counts are not valid" + } + let result = ""; + let counter = count + while (counter > 0) { + result += str + counter--; + } + return result; + } diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index 8a7036102..247ebe846 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -21,12 +21,37 @@ test("should repeat the string count times", () => { // When the repeatStr function is called with these inputs, // Then it should return the original `str` without repetition. +test("should return the original `str` without repetition", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("hello"); +}); + + + // Case: Handle count of 0: // Given a target string `str` and a `count` equal to 0, // When the repeatStr function is called with these inputs, // Then it should return an empty string. +test("should return an empty string", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); + + // Case: Handle negative count: // Given a target string `str` and a negative integer `count`, // When the repeatStr function is called with these inputs, // Then it should throw an error, as negative counts are not valid. + +test("should throw an error, as negative counts are not valid.", () => { + const str = "hello"; + const count = -3; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("Error! negative counts are not valid"); +}); + From e9871d5e04ac9e945caf7c5b42296ab4319acbaf Mon Sep 17 00:00:00 2001 From: Abdennour Hachemi Date: Tue, 13 Oct 2026 10:11:25 +0100 Subject: [PATCH 11/12] solving get-ordinal-number exercise --- practice-tdd/get-ordinal-number.js | 12 +++++++++- practice-tdd/get-ordinal-number.test.js | 32 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index 5c6291f5e..3e82bb441 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,3 +1,13 @@ export function getOrdinalNumber(num) { - return "1st"; + if (String(num).match(/\b(?!11\b)\d*1\b/)) { + return `${num}st`; + } + if (String(num).match(/\b(?!12\b)\d*2\b/)) { + return `${num}nd`; + } + if (String(num).match(/\b(?!13\b)\d*3\b/)) { + return `${num}rd`; + } + return `${num}th` + } diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index e23174a18..1a8ec6717 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,35 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +// Case 2: Numbers ending with 2 (but not 12) +// When the number ends with 2, except those ending with 12, +// Then the function should return a string by appending "nd" to the number. +test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(32)).toEqual("32nd"); + expect(getOrdinalNumber(452)).toEqual("452nd"); +}); + +// Case 3: Numbers ending with 3 (but not 13) +// When the number ends with 3, except those ending with 13, +// Then the function should return a string by appending "rd" to the number. +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(43)).toEqual("43rd"); + expect(getOrdinalNumber(823)).toEqual("823rd"); +}); + +// Case 4: Numbers ending with any other number (but not 1,2 or 3) +// When the number ends with 4,5,6 ,7 ,8 or 9 , +// Then the function should return a string by appending "th" to the number. +test("should append 'th' for numbers ending with 4,5,6 ,7 ,8 or 9 ", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(25)).toEqual("25th"); + expect(getOrdinalNumber(656)).toEqual("656th"); + expect(getOrdinalNumber(1007)).toEqual("1007th"); + expect(getOrdinalNumber(638398)).toEqual("638398th"); + expect(getOrdinalNumber(34454559)).toEqual("34454559th"); +}); + + From 17ad79dd7b14449741fb78dab6dc29073c3632fd Mon Sep 17 00:00:00 2001 From: Abdennour Hachemi Date: Tue, 13 Oct 2026 10:17:31 +0100 Subject: [PATCH 12/12] adding .gitignore file --- practice-tdd/.gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 practice-tdd/.gitignore diff --git a/practice-tdd/.gitignore b/practice-tdd/.gitignore new file mode 100644 index 000000000..ad5e45ccc --- /dev/null +++ b/practice-tdd/.gitignore @@ -0,0 +1,3 @@ +node_modules +package.json +package-lock.json \ No newline at end of file