Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 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,9 @@ 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 when character is not found ", () => {
const str = "helo world";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});
14 changes: 13 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,17 @@
function getOrdinalNumber(num) {
return "1st";
const lastTwoDigits =num % 100;

// Special cases for 11, 12, and 13
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return num + "th";

}
const lastDigit = num % 10;
if (lastDigit === 1) return num + "st";
if (lastDigit ===2) return num + "nd";
if (lastDigit ===3) 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 @@ -16,5 +16,33 @@ const getOrdinalNumber = require("./get-ordinal-number");
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");
expect(getOrdinalNumber(41)).toEqual("41st");
});

// Case 2: Numbers ending with 2 (but not 12)
test("should append 'nd' for number ending with 2 except those ending with 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(42)).toEqual("42nd");
});

// Case 3: Numbers ending with 3 (but not 13)
test("should append 'rd' for number anding with 3 expect those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(43)).toEqual("43rd");
});

// Case 4: Number ending with 4 (but not 14)
test("should append 'th' for number ending with 4 expcet those ending with 14", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(44)).toEqual("44th");
expect(getOrdinalNumber(54)).toEqual("54th");
});

// Case 5: Number ending with 5 (but not 15)
test("should append 'th' for number ending with 5 except those ending with 15", () => {
expect(getOrdinalNumber(5)).toEqual("5th");
expect(getOrdinalNumber(55)).toEqual("55th");
expect(getOrdinalNumber(65)).toEqual("65th");
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What are so special about 14 and 15?

These two test categories do not yet cover all possible numbers not included in the previous test cases. You can consider just creating one test category for all the numbers that should be appended with "th".
However, you should avoid describing these numbers as "all other numbers" because
when a test fails with such a message, it may be unclear what "other numbers" actually refers to.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hi CJ,
Thank you for the feedback. I have updated the test, removed 14 and 15, and added the special case as required.

4 changes: 2 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(n, str) {
return str.repeat(n);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can this function pass the tests in repeat-str.test.js?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I have fixed it now.


module.exports = repeatStr;
17 changes: 17 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,30 @@ 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 return the original string when count is 1", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hello");
});

// 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 return an empty string when count is 0", () => {
const str = "hello";
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 when count is negative", () => {
const str = "hello";
const count = -1;
expect(() => repeatStr(str, count)).toThrow();
});
Loading