Skip to content
Open
8 changes: 7 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let sum = 0;
for (let char of stringOfCharacters) {
if (char === findCharacter) {
sum++;
}
}
return sum;
}

module.exports = countChar;
7 changes: 7 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,10 @@ 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 occurence", () => {
const str = "asdf";
const char = "l";
const count = countChar(str, char);
expect(count).toEqual(0);
});
12 changes: 11 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,15 @@
function getOrdinalNumber(num) {
return "1st";
let numberToString = String(num);
let numberLastDigit = numberToString[numberToString.length - 1];
let numberLast2Digits = numberToString.slice(numberToString.length - 2);
Comment on lines +3 to +4
Copy link
Contributor

Choose a reason for hiding this comment

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

.slice(-N) is clear enough to indicate it extract the last N characters from a string, regardless of string length.


if (numberLastDigit === "1" && numberLast2Digits !== "11")
return numberToString + "st";
if (numberLastDigit === "2" && numberLast2Digits !== "12")
return numberToString + "nd";
if (numberLastDigit === "3" && numberLast2Digits !== "13")
return numberToString + "rd";
return numberToString + "th";
}

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

// Case 2: Numbers ending with 2 (but not 12)
// When the number ends with 2, except those ending with 12,
// Then the function should return a string by appending "nd" to the number.
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(122)).toEqual("122nd");
});

// Case 3: Numbers ending with 3 (but not 13)
// When the number ends with 3, except those ending with 13,
// Then the function should return a string by appending "rd" to the number.
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(123)).toEqual("123rd");
});

// Case 4: Numbers ending with any number (but not 1,2 or 3)
// When the number ends with any number, except those ending with 1,2 or 3,
// Then the function should return a string by appending "th" to the number.
test("should append 'th' for numbers ending with any number including 11,12 or 13, except those ending with 1,2 or 3", () => {
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(25)).toEqual("25th");
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(4)).toEqual("4th");
});
Comment on lines +43 to +48
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add more test cases to this group to make the coverage more comprehensive?
If the number of cases becomes too large, you can consider organizing them into meaningful subcategories.

5 changes: 3 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
if (count < 0) throw new Error("negative counts are not valid");
return str.repeat(count);
}

module.exports = repeatStr;
15 changes: 15 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,28 @@ test("should repeat the string count times", () => {
// Given a target string `str` and a `count` equal to 1,
// When the repeatStr function is called with these inputs,
// Then it should return the original `str` without repetition.
test("should repeat string count times", () => {
const str = "bye";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("bye");
});

// Case: Handle count of 0:
// Given a target string `str` and a `count` equal to 0,
// When the repeatStr function is called with these inputs,
// Then it should return an empty string.
test("should repeat string count times", () => {
Comment on lines +23 to +34
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you revise these test descriptions to make the failure messages more informative, so it's immediately clear both what caused the test to fail and what the expected outcome was?

const str = "no";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

// Case: Handle negative count:
// Given a target string `str` and a negative integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should throw an error, as negative counts are not valid.
test("should throw an error for negative numbers", () => {
expect(() => repeatStr("str", -2)).toThrow("negative counts are not valid");
});