Skip to content
Open
8 changes: 7 additions & 1 deletion practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] === findCharacter) {
count++;
}
}
return count;
}
8 changes: 8 additions & 0 deletions practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,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 count multiple occurrences of a character", () => {
const str = "bbbbb";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});
15 changes: 13 additions & 2 deletions practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export function getOrdinalNumber(num) {
return "1st";
export function getOrdinalNumber(num) {
const numToString = num.toString()

if (numToString === "11" || numToString === "12" || numToString === "13") {
return `${num}th`
} else if (numToString.at(-1) === "1") {
return `${num}st`;
} else if (numToString.at(-1) === "2") {
return `${num}nd`;
} else if (numToString.at(-1) === "3") {
return `${num}rd`;
} return `${num}th`
}

25 changes: 25 additions & 0 deletions practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,28 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

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(132)).toEqual("132nd");
});

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(133)).toEqual("133rd");
});

test("should append 'th' for numbers 11/12/13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
});

test("should append 'th' for the remaining number", () => {
expect(getOrdinalNumber(20)).toEqual("20th");
expect(getOrdinalNumber(35)).toEqual("35th");
expect(getOrdinalNumber(99)).toEqual("99th");
});

Loading