Skip to content
20 changes: 18 additions & 2 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
if (
typeof stringOfCharacters !== "string" ||
typeof findCharacter !== "string"
) {
return 0;
}
let count = 0;
for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] === findCharacter) {
count++;
}
}
return count;
}

console.log(countChar("aaaaa", "a")); // should return 5
console.log(countChar("bravo", "u")); // should return 0
console.log(countChar("", "a")); // should return 0
console.log(countChar("1-2-3-4-5-", "-")); // should return 5
console.log(countChar("AaAa", "A")); // should return 2
module.exports = countChar;
19 changes: 19 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ test("should count multiple occurrences of a character", () => {
});

// Scenario: No Occurrences
test(`should return zero the character doesn't exist in the string`, () => {
const str = "bravo";
const char = "u";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Empty String
test(`should return zero when the string is empty`, () => {
expect(countChar("", "a")).toEqual(0);
});

// Scenario: Multiple Occurrences
test("should count multiple occurrences of characters (including mixed and case-sensitive)", () => {
expect(countChar("aaaaa", "a")).toEqual(5); // simple multiple
expect(countChar("1-2-3-4-5-", "-")).toEqual(5); // mixed characters
expect(countChar("AaAa", "A")).toEqual(2); // case sensitivity
});

// Given the input string `str`,
// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
Expand Down
19 changes: 18 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,22 @@
function getOrdinalNumber(num) {
return "1st";
if (typeof num !== "number" || !Number.isInteger(num) || num < 0) {
throw new Error("Input must be a non-negative integer");
}

const lastTwo = num % 100;
const lastDigit = num % 10;

// Special cases: 11th, 12th, 13th
if (lastTwo === 11 || lastTwo === 12 || lastTwo === 13) {
return `${num}th`;
}

// Normal cases
if (lastDigit === 1) return `${num}st`;
if (lastDigit === 2) return `${num}nd`;
if (lastDigit === 3) return `${num}rd`;

return `${num}th`;
}

module.exports = getOrdinalNumber;
29 changes: 29 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 @@ -13,8 +13,37 @@ const getOrdinalNumber = require("./get-ordinal-number");
// Case 1: Numbers ending with 1 (but not 11)
// When the number ends with 1, except those ending with 11,
// Then the function should return a string by appending "st" to the 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");
});

// Case 2: Numbers ending with 2 (but NOT 12)
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(102)).toEqual("102nd");
});

// Case 3: Numbers ending with 3 (but NOT 13)
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(103)).toEqual("103rd");
});

// Case 4: Numbers ending with 11, 12, or 13 --- 11,12,13,111,121,131,
test("should append 'th' for numbers ending with 11, 12, 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
});

// Case 5: Numbers ending with larger numbers of 111, 121, 131
test("should append 'th' for numbers ending with 111, 112, 113", () => {
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(113)).toEqual("113th");
});
29 changes: 25 additions & 4 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
function repeatStr() {
// 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";
// 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.
function repeatStr(str, count) {
if (typeof str !== "string") {
return "";
}
if (count < 0) {
throw new Error("Invalid input: count must be a non-negative integer");
}

let result = "";

for (let i = 0; i < count; i++) {
result += str;
}

return result;
}
console.log(repeatStr("hello", 3)); // Output: "hellohellohello"
console.log(repeatStr("fella", 1)); // Output: "fella"
console.log(repeatStr("fella", 0)); // Output: ""
console.log(repeatStr("ab", 3)); // Output: "ababab"
console.log(repeatStr("a", 1000).length); // Output: 1000
console.log(repeatStr("", 5)); // Output: ""
console.log(repeatStr([], 3)); // Output: ""
console.log(repeatStr(null, 3)); // Output: ""

module.exports = repeatStr;
32 changes: 32 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.

You have correctly tested the edge cases here (1, 0, error). Can you think of any other test cases that would make sense?

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.

yes, I've now added tests for null, "", non-string inputs and large repeat counts. Could you please now review it? Thanks for your patience

Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,48 @@ test("should repeat the string count times", () => {
});

// Case: handle count of 1:
test("should repeat the string count of 1", () => {
const str = "fella";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("fella");
});

// 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.

// Case: Handle count of 0:
test("should repeat the string count of 0", () => {
const str = "fella";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

// 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.

// Case: Handle negative count:
test(`should throw an error for negative count`, () => {
expect(() => repeatStr("fella", -2)).toThrowError(
"Invalid input: count must be a non-negative integer"
);
});
// 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 return empty string for non-string input", () => {
expect(repeatStr([], 3)).toEqual("");
expect(repeatStr(null, 3)).toEqual("");
});
test("should handle large repeat counts", () => {
expect(repeatStr("a", 1000).length).toEqual(1000);
});
test("should repeat a multi-character string multiple times", () => {
expect(repeatStr("ab", 3)).toEqual("ababab");
});
test("should return an empty string when repeating an empty string", () => {
expect(repeatStr("", 5)).toEqual("");
});
Loading