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; 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); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..7fa110ed0c 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,43 @@ -function getOrdinalNumber(num) { - return "1st"; +//function getOrdinalNumber(num) { + //return "1st"; +//} + +//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) { + // 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; 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..c24cdf424e 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,61 @@ 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 + +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: 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`); + } + ); + }); + }); +});