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
17 changes: 17 additions & 0 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,20 @@ function countChar(stringOfCharacters, findCharacter) {
}

module.exports = countChar;
//

//Below is the implementation of the countChar function that counts the number of times a character occurs in a string.
// The function takes two parameters: `stringOfCharacters`, which is the string to search through, and `findCharacter`, which is the character to count.
// The function initializes a counter to 0 and iterates through each character in the string. If the current character matches `findCharacter`, the counter is incremented. Finally, the function returns the total count.

function countChar(stringOfCharacters, findCharacter) {
let count = 0;
for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] === findCharacter) {
count++;
}
}
return count;
}

module.exports = countChar;
7 changes: 7 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,10 @@ 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 occur in the string", () => {
const str = "hello world";
const char = "x";
const count = countChar(str, char);
expect(count).toEqual(0);
} );

25 changes: 25 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,28 @@ function getOrdinalNumber(num) {
}

module.exports = getOrdinalNumber;
// Below is the implementation of the getOrdinalNumber function that converts a number to its ordinal form (e.g., 1 to "1st", 2 to "2nd", etc.).
// The function takes a single parameter `num`, which is the number to convert.
// It checks the last digit of the number to determine the appropriate suffix ("st", "nd", "rd", or "th") and returns the number concatenated with its ordinal suffix as a string.

function getOrdinalNumber(num) {
const lastDigit = num % 10;
const lastTwoDigits = num % 100;

if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return num + "th";
}

switch (lastDigit) {
case 1:
return num + "st";
case 2:
return num + "nd";
case 3:
return num + "rd";
default:
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 @@ -18,3 +18,32 @@ 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 0, 4-9, or 10-20 (including 11-19)
// When the number ends with 0, 4-9, or 10-20 (including 11-19),
// Then the function should return a string by appending "th" to the number.
test("should append 'th' for numbers ending with 0, 4-9, or 10-20 (including 11-19)", () => {
expect(getOrdinalNumber(0)).toEqual("0th");
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(5)).toEqual("5th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(20)).toEqual("20th");
});
8 changes: 8 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ function repeatStr() {
}

module.exports = repeatStr;
// Below is the implementation of the repeatStr function that takes a string and a number as parameters and returns the string repeated the specified number of times.
// The function uses the built-in `repeat` method of strings to achieve this. It takes the input string and repeats it `n` times, where `n` is the number provided as the second parameter.

function repeatStr(str, n) {
return str.repeat(n);
}

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("Count must be a non-negative integer");
});
Loading