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

for (let count = 0; count < stringOfCharacters.length; count++) {
if (stringOfCharacters[count] === findCharacter) {
charcount++;
}
}

return charcount;
}

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 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("test for countchar with 0 occurrences", () => {
const str = "hello world";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});
18 changes: 16 additions & 2 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
function getOrdinalNumber(num) {
return "1st";
}
const lastDigit = num % 10;
const lastTwoDigit = num % 100;

if (lastTwoDigit >= 11 && lastTwoDigit <= 13) {
return `${num}th`;
}

if (lastDigit === 1) {
return `${num}st`;
} else if (lastDigit === 2) {
return `${num}nd`;
} else if (lastDigit === 3) {
return `${num}rd`;
} else {
return `${num}th`;
}
}
module.exports = getOrdinalNumber;
19 changes: 19 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,22 @@ const getOrdinalNumber = require("./get-ordinal-number");
test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});

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

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

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

test("should return '12th' for 12", () => {
expect(getOrdinalNumber(12)).toEqual("12th");
});
test("should return '13th' for 13", () => {
expect(getOrdinalNumber(13)).toEqual("13th");
});
8 changes: 6 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {
if (count < 0) {
throw new Error("negative counts are not valid.");
}

return str.repeat(count);
}

module.exports = repeat;
17 changes: 17 additions & 0 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,29 @@ test("should repeat the string count times", () => {
// When the repeat 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 repeat the string count times", () => {
const str = "world";
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("world");
});

// case: Handle Count of 0:
// Given a target string str and a count equal to 0,
// When the repeat 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 repeat the string count times", () => {
const str = "hi";
const count = 0;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("");
});
// case: Negative Count:
// Given a target string str and a negative integer count,
// When the repeat 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 throw an error for negative count", () => {
expect(() => repeat("no", -1)).toThrow("negative counts are not valid.");
});