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

return totalChar.length - 1;
}

module.exports = countChar;
9 changes: 9 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ test("should count multiple occurrences of a character", () => {
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
expect(countChar("banana", "n")).toEqual(2);
expect(countChar("=^.^=", "^")).toEqual(2);
expect(countChar("=^.^=", ".")).toEqual(1);
});

// Scenario: No Occurrences
// Given the input string str,
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
test("should check for no occurrences of a character", () => {
const str = "abcdef";
const char = "g";
const count = countChar(str, char);
expect(count).toEqual(0);
});
30 changes: 29 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,33 @@
function getOrdinalNumber(num) {
return "1st";
const lastDigit = num.toString()[num.toString().length - 1];
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 using the more efficient approach involving the % operator to extract the last digit and the last two digits from a number directly.


if (lastDigit === "1") {
if (num === 11) {
return `${num}th`;
}

return `${num}st`;
} else if (lastDigit === "2") {
if (num.toString().length > 1) {
const last2Digits = num.toString().slice(-2);

if (last2Digits === "12") {
return `${num}th`;
}
}
return `${num}nd`;
} else if (lastDigit === "3") {
if (num.toString().length > 1) {
const last2Digits = num.toString().slice(-2);

if (last2Digits === "13") {
return `${num}th`;
}
}
return `${num}rd`;
}

return `${num}th`;
}

module.exports = getOrdinalNumber;
30 changes: 29 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 @@ -8,6 +8,34 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {
test("should append 'st' to numbers with 1 at the end except for those ending with 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(101)).toEqual("101st");
expect(getOrdinalNumber(151)).toEqual("151st");
expect(getOrdinalNumber(2061)).toEqual("2061st");
});

test("should append 'nd' to numbers with 2 at the end except for those ending with 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(342)).toEqual("342nd");
expect(getOrdinalNumber(592)).toEqual("592nd");
expect(getOrdinalNumber(1972)).toEqual("1972nd");
});

test("should append 'rd' to numbers with 3 at the end except for those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(353)).toEqual("353rd");
expect(getOrdinalNumber(93)).toEqual("93rd");
expect(getOrdinalNumber(783)).toEqual("783rd");
});

test("should append 'th' to all other numbers which do not end in 1,2,3,11,12 or 13", () => {
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(212)).toEqual("212th");
expect(getOrdinalNumber(113)).toEqual("113th");
expect(getOrdinalNumber(17)).toEqual("17th");
});
Loading