Skip to content
Closed
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
22 changes: 19 additions & 3 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


/**
* Counts the occurrences of a character within a string.
* @param {string} str - The string to search through.
* @param {string} char - The character to look for.
* @returns {number} - The total count of the character.
*/
function countChar(str, char) {
let count = 0;

for (const letter of str) {
if (letter === char) {
count++;
}
}

return count;
}

module.exports = countChar;
module.exports = countChar;
37 changes: 37 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// implement a function countChar that counts the number of times a character occurs in a string

/*
const countChar = require("./count");
*/


// Given a string `str` and a single character `char` to search for,
// When the countChar function is called with these inputs,
// Then it should:
Expand All @@ -10,15 +15,47 @@ const countChar = require("./count");
// When the function is called with these inputs,
// Then it should correctly count occurrences of `char`.

/*
test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
});
*/

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



const countChar = require("./count");

describe("countChar function", () => {
// Scenario: Multiple Occurrences
test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
});

// Scenario: No Occurrences
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
});
});
26 changes: 25 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,29 @@


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;


52 changes: 37 additions & 15 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@

const getOrdinalNumber = require("./get-ordinal-number");
// In this week's prep, we started implementing getOrdinalNumber.

// Continue testing and implementing getOrdinalNumber for additional cases.
// Write your tests using Jest — remember to run your tests often for continual feedback.
describe("getOrdinalNumber", () => {
// Case 1: Numbers ending with 1 (but not 11)
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
expect(getOrdinalNumber(1)).toBe("1st");
expect(getOrdinalNumber(21)).toBe("21st");
expect(getOrdinalNumber(101)).toBe("101st");
});

// 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");
});

// To ensure thorough testing, we need broad scenarios that cover all possible cases.
// Listing individual values, however, can quickly lead to an unmanageable number of test cases.
// Instead of writing tests for individual numbers, consider grouping all possible input values
// into meaningful categories. Then, select representative samples from each category to test.
// This approach improves coverage and makes our tests easier to maintain.
// 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 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 5: All other numbers (Ending in 0, 4, 5, 6, 7, 8, 9)
test("should append 'th' for all other numbers", () => {
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");
});
});
19 changes: 17 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
function repeatStr() {
return "hellohellohello";


/**
* Repeats a string a given number of times.
* @param {string} str - The string to repeat.
* @param {number} count - The number of times to repeat the string.
* @returns {string} - The repeated string.
* @throws {Error} - If count is a negative integer.
*/
function repeatStr(str, count) {
if (count < 0) {
throw new Error("Count must be a non-negative integer");
}

// If count is 0, .repeat(0) naturally returns an empty string "".
// If count is 1, .repeat(1) naturally returns the original string.
return str.repeat(count);
}

module.exports = repeatStr;
47 changes: 47 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@

const repeatStr = require("./repeat-str");

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 0
test("should return an empty string when count is 0", () => {
const str = "ghost";
const count = 0;
expect(repeatStr(str, count)).toEqual("");
});

// 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");
});
});


// 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:
Expand All @@ -9,13 +52,17 @@ const repeatStr = require("./repeat-str");
// 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");
});

*/


// 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,
Expand Down
Loading