Skip to content
Open
7 changes: 6 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
if (findCharacter == "") return 0;

const string = stringOfCharacters.toLowerCase();
const char = findCharacter.toLowerCase();

return string.split(char).length - 1;
}

module.exports = countChar;
22 changes: 22 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,25 @@ 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 return 0 for no occurances of 'char' in 'str'`, () => {
const str = "code your future";
const char = "x";
const count = countChar(str, char);
expect(count).toEqual(0);
})
Comment on lines +25 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Could consider testing more samples.

  • Could consider testing these cases:

    • A case to show that the match is case sensitive
    • A case to show that the function is expected to work also for non-alphabets

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, sir, for the review. I fixed all the suggested changes.


test(`in case sensitive case, should also return the number of character in the string`, () => {
expect(countChar("Code Your Future!", "y")).toEqual(1);
});

Comment on lines +32 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could include a lower case y in the input to demonstrate and ensure the match is indeed case-sensitive.

test(`should return the count of character for non-alphabests`, () => {
expect(countChar("86421", "1")).toEqual(1);
});

test(`should return 0 for empty strings`, () => {
expect(countChar("code your future", "")).toEqual(0)
});

test(`should return the count for special characters`, () => {
expect(countChar("code& $future £your", "$")).toEqual(1)
})
16 changes: 15 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
function getOrdinalNumber(num) {
return "1st";
const lastDigit = num % 10;
const lastTwoDigits = num % 100;

if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) {
return `${num}th`;
} else if (lastDigit === 1) {
return `${num}st`;
} else if (lastDigit === 2) {
return `${num}nd`;
} else if (lastDigit === 3) {
return `${num}rd`;
} else {
return `${num}th`;

}
}

module.exports = getOrdinalNumber;
33 changes: 32 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,39 @@ const getOrdinalNumber = require("./get-ordinal-number");
// Case 1: Numbers ending with 1 (but not 11)
// When the number ends with 1, except those ending with 11,
// Then the function should return a string by appending "st" to the number.
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
test("appends 'st' for numbers ending in 1 except 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

test("appends 'nd' for numbers ending in 2 except 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(92)).toEqual("92nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
expect(getOrdinalNumber(1142)).toEqual("1142nd");
})

test("appends 'rd' for numbers ending in 3 except 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(63)).toEqual("63rd");
expect(getOrdinalNumber(533)).toEqual("533rd");
})

test("appends 'th' when number does not end in 1, 2, or 3", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(14)).toEqual("14th");
expect(getOrdinalNumber(20)).toEqual("20th");
expect(getOrdinalNumber(99)).toEqual("99th");
expect(getOrdinalNumber(100)).toEqual("100th");
expect(getOrdinalNumber(670)).toEqual("670th");
})

test("appends 'th' for numbers ending in 11, 12, or 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
})
Loading