From 0dc502dea8bb880678c21ef116315c9df729287a Mon Sep 17 00:00:00 2001 From: Ogbemi mene Date: Fri, 10 Jul 2026 17:30:40 +0100 Subject: [PATCH 1/6] count function --- Sprint-3/2-practice-tdd/count.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..79977793e5 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,13 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + count++; + } + } + + return count; } module.exports = countChar; From 9733fa31dce61e8c80a8cba4db9a6cf5f2ef01aa Mon Sep 17 00:00:00 2001 From: Ogbemi mene Date: Fri, 10 Jul 2026 17:47:32 +0100 Subject: [PATCH 2/6] count.test answer --- Sprint-3/2-practice-tdd/count.test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..3400c0f729 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,13 @@ 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. + +// to solve this issue, we will implement a test case that checks for the scenario where the character is not found in the string. +// The test will call the countChar function with a string and a character that does not exist in that string, and it will assert that the returned count is 0. +// here is the code for the test case: +test("should return 0 when character is not found", () => { + const str = "hello"; + const char = "x"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); From 7991a9b5ae9245f010d3772b4259ba5e6490f8bd Mon Sep 17 00:00:00 2001 From: Ogbemi mene Date: Fri, 10 Jul 2026 17:59:47 +0100 Subject: [PATCH 3/6] getting ordinal num --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..80c6aefc5f 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -3,3 +3,12 @@ function getOrdinalNumber(num) { } module.exports = getOrdinalNumber; +// this is a code i came up with to get the ordinal number of a given number. +// I will now write tests to check if it works correctly. + +function getOrdinalNumber(n) { + const pr = new Intl.PluralRules("en-US", { type: "ordinal" }); + const suffixes = { one: "st", two: "nd", few: "rd", other: "th" }; + const rule = pr.select(n); + return `${n}${suffixes[rule]}`; +} \ No newline at end of file From 0e7fe7030bfe5100c58752b30395449b0160f7b3 Mon Sep 17 00:00:00 2001 From: Ogbemi mene Date: Fri, 10 Jul 2026 18:00:14 +0100 Subject: [PATCH 4/6] test.js for ordinal num --- .../2-practice-tdd/get-ordinal-number.test.js | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index adfa58560f..4d3aac8eee 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,49 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +// here are more test cases for different scenarios + +const getOrdinalNumber = require("./get-ordinal-number"); + +describe("getOrdinalNumber", () => { + // Case 1: Already defined in your prompt + test("should append 'st' for numbers ending with 1, except those ending with 11", () => { + expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(131)).toEqual("131st"); + }); + + // Case 2: Numbers ending with 2 (but not 12) + test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(142)).toEqual("142nd"); + }); + + // Case 3: Numbers ending with 3 (but not 13) + test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(153)).toEqual("153rd"); + }); + + // Case 4: Numbers ending with 11, 12, 13 (The exceptions) + test("should append 'th' for numbers ending in 11, 12, or 13", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(111)).toEqual("111th"); + expect(getOrdinalNumber(112)).toEqual("112th"); + expect(getOrdinalNumber(113)).toEqual("113th"); + }); + + // Case 5: All other numbers + test("should append 'th' for all other numbers", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(20)).toEqual("20th"); + expect(getOrdinalNumber(34)).toEqual("34th"); + expect(getOrdinalNumber(100)).toEqual("100th"); + }); +}); \ No newline at end of file From 046340759680a70679407a6487eb772dda0808fd Mon Sep 17 00:00:00 2001 From: Ogbemi Mene Date: Tue, 21 Jul 2026 08:14:41 +0100 Subject: [PATCH 5/6] the new code without API( application programm interface). --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 47 +++++++++++++++---- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index 80c6aefc5f..7fa110ed0c 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,14 +1,43 @@ -function getOrdinalNumber(num) { - return "1st"; -} +//function getOrdinalNumber(num) { + //return "1st"; +//} -module.exports = getOrdinalNumber; +//module.exports = getOrdinalNumber; // this is a code i came up with to get the ordinal number of a given number. // I will now write tests to check if it works correctly. +//function getOrdinalNumber(n) { + //const pr = new Intl.PluralRules("en-US", { type: "ordinal" }); + //const suffixes = { one: "st", two: "nd", few: "rd", other: "th" }; + //const rule = pr.select(n); + //return `${n}${suffixes[rule]}`; +//} + +// code without any API + function getOrdinalNumber(n) { - const pr = new Intl.PluralRules("en-US", { type: "ordinal" }); - const suffixes = { one: "st", two: "nd", few: "rd", other: "th" }; - const rule = pr.select(n); - return `${n}${suffixes[rule]}`; -} \ No newline at end of file + // Pure arithmetic to get absolute value without Math.abs() + let positiveN; +if (n < 0) { + positiveN = -n; +} else { + positiveN = n; +} + + const lastTwoDigits = positiveN % 100; + const lastDigit = positiveN % 10; + + // Teens exception: 11th, 12th, 13th + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return `${n}th`; + } + + // Standard endings + if (lastDigit === 1) return `${n}st`; + if (lastDigit === 2) return `${n}nd`; + if (lastDigit === 3) return `${n}rd`; + + return `${n}th`; +} + +module.exports = getOrdinalNumber; From 4e621d48372ab82f1e1145bb2b0c273d1260373c Mon Sep 17 00:00:00 2001 From: Ogbemi Mene Date: Tue, 21 Jul 2026 09:09:39 +0100 Subject: [PATCH 6/6] Update test cases for getOrdinalNumber function Refactor tests for getOrdinalNumber to include cases for numbers ending in 0, 4, 5, 6, 7, 8, or 9. --- .../2-practice-tdd/get-ordinal-number.test.js | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index 4d3aac8eee..c24cdf424e 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -21,8 +21,6 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" // here are more test cases for different scenarios -const getOrdinalNumber = require("./get-ordinal-number"); - describe("getOrdinalNumber", () => { // Case 1: Already defined in your prompt test("should append 'st' for numbers ending with 1, except those ending with 11", () => { @@ -55,12 +53,26 @@ describe("getOrdinalNumber", () => { expect(getOrdinalNumber(113)).toEqual("113th"); }); - // Case 5: All other numbers - test("should append 'th' for all other numbers", () => { - expect(getOrdinalNumber(4)).toEqual("4th"); - expect(getOrdinalNumber(10)).toEqual("10th"); - expect(getOrdinalNumber(20)).toEqual("20th"); - expect(getOrdinalNumber(34)).toEqual("34th"); - expect(getOrdinalNumber(100)).toEqual("100th"); + // Case 5: Numbers ending in 0, 4, 5, 6, 7, 8, or 9 + describe("numbers ending with 0, 4, 5, 6, 7, 8, or 9", () => { + const defaultThCases = [ + { endingDigit: 0, testValues: [0, 10, 20, 100, -10] }, + { endingDigit: 4, testValues: [4, 24, 104, -4] }, + { endingDigit: 5, testValues: [5, 25, 105, -5] }, + { endingDigit: 6, testValues: [6, 26, 106, -6] }, + { endingDigit: 7, testValues: [7, 27, 107, -7] }, + { endingDigit: 8, testValues: [8, 28, 108, -8] }, + { endingDigit: 9, testValues: [9, 29, 109, -9] }, + ]; + + + defaultThCases.forEach(({ endingDigit, testValues }) => { + test.each(testValues)( + `appends "th" to %i (representative for digit ${endingDigit})`, + (num) => { + expect(getOrdinalNumber(num)).toBe(`${num}th`); + } + ); + }); }); -}); \ No newline at end of file +});