Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion Sprint-3/2-practice-tdd/count.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a wonderfully clean and expressive solution! Using array methods like .split() and .filter() makes the logic very readable and easy to follow.

Quick question: while this functional approach is great for readability, what happens if you pass a very large string? Can you think of an alternative way to count characters (like using a loop or for...of) that avoids creating an intermediate array?

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
return stringOfCharacters.split("").filter((char) => char === findCharacter)
.length;
}

module.exports = countChar;
13 changes: 13 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fantastic test suite! Using Jest (test, expect) and covering clear scenarios like multiple occurrences and zero matches makes the code very robust. Great job

Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,21 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

test("should count multiple occurrences of a different character", () => {
const str = "banana";
const char = "n";
const count = countChar(str, char);
expect(count).toEqual(2);
});

// Scenario: No Occurrences
// Given the input string `str`,
// 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 no occurrences of a character are found", () => {
const str = "aaaaa";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(0);
});
21 changes: 20 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fantastic implementation! Handling the special cases for 11, 12, and 13 along with robust input validation makes this code exceptionally reliable.

Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
function getOrdinalNumber(num) {
return "1st";
// check if the input is not a number, not an integer, or if it's negative
if (typeof num !== "number" || !Number.isInteger(num) || num < 0) {
Comment thread
tomdu3 marked this conversation as resolved.
throw new Error("Invalid input: number must be a non-negative integer");
Comment thread
tomdu3 marked this conversation as resolved.
}

if (num % 100 >= 11 && num % 100 <= 13) {
// check for numbers ending with 11, 12, or 13
return num + "th";
} else if (num % 10 === 1) {
// check for numbers ending with 1 (but not 11)
return num + "st";
} else if (num % 10 === 2) {
// check for numbers ending with 2 (but not 12)
return num + "nd";
} else if (num % 10 === 3) {
// check for numbers ending with 3 (but not 13)
return num + "rd";
}
// For all other numbers, append "th"
return num + "th";
}

module.exports = getOrdinalNumber;
65 changes: 65 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fantastic test suite! Grouping tests into meaningful categories (such as special endings, edge cases, negative numbers, and non-number inputs) provides exceptional test coverage. Great work

Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,68 @@ 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(132)).toEqual("132nd");
});

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

// Case 4: Numbers ending with 11, 12, or 13
// When the number ends with 11, 12, or 13,
// Then the function should return a string by appending "th" to the number.
test("should append 'th' for numbers ending with 11, 12, or 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
});

// Case 5: All other numbers
// When the number does not fall into any of the above categories,
// Then the function should return a string by appending "th" to the number.
test("should append 'th' for all other numbers", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(5)).toEqual("5th");
expect(getOrdinalNumber(6)).toEqual("6th");
expect(getOrdinalNumber(7)).toEqual("7th");
expect(getOrdinalNumber(8)).toEqual("8th");
expect(getOrdinalNumber(9)).toEqual("9th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(127)).toEqual("127th");
});

// Case 6: Negative numbers
// When the number is negative,
// Then the function should throw an error, as negative numbers are not valid inputs.
test("should throw an error for negative numbers", () => {
expect(() => getOrdinalNumber(-1)).toThrow("Invalid input: number must be a non-negative integer");
expect(() => getOrdinalNumber(-22)).toThrow("Invalid input: number must be a non-negative integer");
});

// Case 7: Non-integer numbers
// When the number is not an integer (e.g., a float),
// Then the function should throw an error, as only non-negative integers are valid inputs.
test("should throw an error for non-integer numbers", () => {
expect(() => getOrdinalNumber(1.5)).toThrow("Invalid input: number must be a non-negative integer");
expect(() => getOrdinalNumber(2.7)).toThrow("Invalid input: number must be a non-negative integer");
});

// Case 8: Non-number inputs
// When the input is not a number (e.g., a string, object, or array),
// Then the function should throw an error, as only non-negative integers are valid inputs.
test("should throw an error for non-number inputs", () => {
expect(() => getOrdinalNumber("1")).toThrow("Invalid input: number must be a non-negative integer");
expect(() => getOrdinalNumber("abcd")).toThrow("Invalid input: number must be a non-negative integer");
});
10 changes: 7 additions & 3 deletions Sprint-3/2-practice-tdd/repeat-str.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fantastic recursive implementation! Avoiding String.prototype.repeat and using recursion to build the string while properly handling validation and edge cases is brilliant.

Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
function repeatStr() {
function repeatStr(stringOfCharacters, count) {
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
// The goal is to re-implement that function, not to use it.
return "hellohellohello";
if (count === 0) {
return "";
} else if (count > 0) {
return repeatStr(stringOfCharacters, count - 1) + stringOfCharacters;
}
throw Error("Invalid input: count must be a non-negative integer");
}

module.exports = repeatStr;
20 changes: 20 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fantastic test suite! Covering multiple repetitions, edge cases like 0 and 1, and negative input error handling ensures robust test coverage.

Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,32 @@ test("should repeat the string count times", () => {
// 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 = "world";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("world");
});

// 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 = "test";
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 = "error";
const count = -2;
expect(() => repeatStr(str, count)).toThrow("Invalid input: count must be a non-negative integer");
});
Loading