Skip to content
6 changes: 5 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
const arrToStr = stringOfCharacters.split("");

const countCharInArr = arrToStr.filter((char) => char === findCharacter);

return countCharInArr.length;
}

module.exports = countChar;
6 changes: 6 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,9 @@ test("should count multiple occurrences of a character", () => {
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
test("should return 0 if there's no occurrences of the char were found in the case-sensitive str", () => {
const str = "aaaaABcGRja";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(0);
});
13 changes: 12 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,16 @@
function getOrdinalNumber(num) {
return "1st";
const lastDigit = num % 10;
const lastTwoDigits = num % 100;

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

if (lastDigit === 1) return num + "st";
if (lastDigit === 2) return num + "nd";
if (lastDigit === 3) return num + "rd";

return num + "th";
}

module.exports = getOrdinalNumber;
36 changes: 36 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 @@ -11,3 +11,39 @@ const getOrdinalNumber = require("./get-ordinal-number");
test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});

// Case 2: Identify the ordinal number for 2
// When the number is 2,
// Then the function should return "2nd"

test("should return '2nd' for 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
});

// Case 3: Identify the ordinal number for 3
// When the number is 3,
// Then the function should return "3rd"

test("should return '3rd' for 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
});

// Case 4: Identify the ordinal number for other numbers
// If the last digit of number is not 1, 2, 3
// and the last two digits are not between 11 and 13
// Then the function should return the number with "th" suffix
// otherwise, return the number with the appropriate suffix

test("should return '44th' for 44; '131st' for 131 etc.", () => {
expect(getOrdinalNumber(44)).toEqual("44th");
expect(getOrdinalNumber(9)).toEqual("9th");
expect(getOrdinalNumber(20)).toEqual("20th");
expect(getOrdinalNumber(100)).toEqual("100th");
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(113)).toEqual("113th");
expect(getOrdinalNumber(131)).toEqual("131st");
expect(getOrdinalNumber(222)).toEqual("222nd");
expect(getOrdinalNumber(323)).toEqual("323rd");
expect(getOrdinalNumber(4046486655)).toEqual("4046486655th");
});
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";
function repeatStr(str, count) {
if (count === 0) {
return "";
}
if (count < 0) {
return "Error! Negative counts are not valid!";
}

let repeatedStrCountTimes = "";

let i = 0;
while (i < count) {
repeatedStrCountTimes += str;
i++;
}

return repeatedStrCountTimes;
}

module.exports = repeatStr;
18 changes: 18 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,31 @@ 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, ensuring that a count of 1 results in no repetition.
test("should return the original string without repetition if count equal to 1", () => {
const str = "Alone";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("Alone");
});

// 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, ensuring that a count of 0 results in an empty output.
test("should return an empty string if count equal to 0", () => {
const str = "nonsense";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

// case: 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 or return an appropriate error message, as negative counts are not valid.
test("should return an appropriate error message if count equal to a negative integer", () => {
const str = "negative";
const count = -5;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("Error! Negative counts are not valid!");
});