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
11 changes: 10 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;

for (const letter of stringOfCharacters) {
if (letter === findCharacter) {
count++;
}
}

return count;

}

module.exports = countChar;
14 changes: 14 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,17 @@ 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 the character does not exist in the string", () => {
const str = "hello";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Bonus Scenario: Mixed Case (Character searching is case-sensitive)
test("should be case-sensitive", () => {
const str = "Abba";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(1); // Only the lowercase 'a' is counted
});
24 changes: 23 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,27 @@

function getOrdinalNumber(num) {
return "1st";
const s = num.toString();
const lastDigit = num % 10;
const lastTwoDigits = num % 100;

// Rule for 11th, 12th, 13th (The "Teens" exception)
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return s + "th";
}

// Standard rules based on the last digit
switch (lastDigit) {
case 1:
return s + "st";
case 2:
return s + "nd";
case 3:
return s + "rd";
default:
return s + "th";
}
}



module.exports = getOrdinalNumber;
44 changes: 39 additions & 5 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,42 @@ 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");
});

describe("getOrdinalNumber", () => {
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)).toBe("2nd");
expect(getOrdinalNumber(42)).toBe("42nd");
expect(getOrdinalNumber(122)).toBe("122nd");
});

// 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)).toBe("3rd");
expect(getOrdinalNumber(53)).toBe("53rd");
expect(getOrdinalNumber(1003)).toBe("1003rd");
});

// Case 4: The Teen Exceptions (11, 12, 13)
test("should append 'th' for numbers ending in 11, 12, or 13", () => {
expect(getOrdinalNumber(11)).toBe("11th");
expect(getOrdinalNumber(12)).toBe("12th");
expect(getOrdinalNumber(13)).toBe("13th");
expect(getOrdinalNumber(111)).toBe("111th"); // Edge case: triple digits
});

// Case 5: All other numbers (Ending in 0, 4, 5, 6, 7, 8, 9)
test("should append 'th' for numbers ending in 0, or 4 through 9", () => {
expect(getOrdinalNumber(0)).toBe("0th");
expect(getOrdinalNumber(4)).toBe("4th");
expect(getOrdinalNumber(9)).toBe("9th");
expect(getOrdinalNumber(10)).toBe("10th");
expect(getOrdinalNumber(20)).toBe("20th");
});
});
10 changes: 8 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
if (count < 0) {
throw new Error("Count must be a non-negative integer");
}


return str.repeat(count);

}

module.exports = repeatStr;
57 changes: 32 additions & 25 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
// Implement a function repeatStr
const repeatStr = require("./repeat-str");
// Given a target string `str` and a positive integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should:

// Case: handle multiple repetitions:
// Given a target string `str` and a positive integer `count` greater than 1,
// When the repeatStr function is called with these inputs,
// Then it should return a string that contains the original `str` repeated `count` times.

test("should repeat the string count times", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohellohello");
});
describe("repeatStr", () => {
// Case: handle multiple repetitions
test("should repeat the string count times", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohellohello");
});

// Case: handle count of 1
test("should return the original string when count is 1", () => {
const str = "apple";
const count = 1;
expect(repeatStr(str, count)).toEqual("apple");
});

// Case: handle count of 1:
// 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 return an empty string when count is 0", () => {
const str = "ghost";
const count = 0;
expect(repeatStr(str, count)).toEqual("");
});

// 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.
// Case: Handle negative count
test("should throw an error when count is negative", () => {
const str = "error";
const count = -1;

// We wrap the call in a function so Jest can catch the error
expect(() => {
repeatStr(str, count);
}).toThrow("Count must be a non-negative integer");
});
});

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