Skip to content

Commit f1bb8ed

Browse files
committed
Completed all exercises in 2-practice-tdd directory
1 parent c64bb45 commit f1bb8ed

File tree

6 files changed

+78
-5
lines changed

6 files changed

+78
-5
lines changed

Sprint-3/2-practice-tdd/count.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let count = 0;
3+
for (let i = 0; i < stringOfCharacters.length; i++) {
4+
if (stringOfCharacters[i] === findCharacter) {
5+
count++;
6+
}
7+
}
8+
return count;
39
}
410

511
module.exports = countChar;

Sprint-3/2-practice-tdd/count.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character `char` that does not exist within `str`.
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of `char` were found.
25+
26+
test("should return 0 when character does not exist in the string", () => {
27+
const str = "hello world";
28+
const char = "x";
29+
const count = countChar(str, char);
30+
expect(count).toEqual(0);
31+
});
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
let stringNum = num.toString();
3+
let lastDigit = stringNum[stringNum.length - 1];
4+
if (lastDigit === "1" && num % 100 !== 11) {
5+
return stringNum + "st";
6+
} else if (lastDigit === "2" && num % 100 !== 12) {
7+
return stringNum + "nd";
8+
} else if (lastDigit === "3" && num % 100 !== 13) {
9+
return stringNum + "rd";
10+
} else {
11+
return stringNum + "th";
12+
}
313
}
414

515
module.exports = getOrdinalNumber;

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,29 @@ const getOrdinalNumber = require("./get-ordinal-number");
1616
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
1717
expect(getOrdinalNumber(1)).toEqual("1st");
1818
expect(getOrdinalNumber(21)).toEqual("21st");
19-
expect(getOrdinalNumber(31)).toEqual("131st");
19+
expect(getOrdinalNumber(31)).toEqual("31st");
20+
expect(getOrdinalNumber(131)).toEqual("131st");
21+
//expect(getOrdinalNumber(31)).toEqual("131st"); Assuming this is a typo and should be 31st
22+
});
23+
24+
// Case 2: Numbers ending with 2
25+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
26+
expect(getOrdinalNumber(2)).toEqual("2nd");
27+
expect(getOrdinalNumber(12)).toEqual("12th");
28+
expect(getOrdinalNumber(22)).toEqual("22nd");
29+
expect(getOrdinalNumber(132)).toEqual("132nd");
30+
});
31+
32+
// Case 3: Numbers ending with 3
33+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
34+
expect(getOrdinalNumber(3)).toEqual("3rd");
35+
expect(getOrdinalNumber(13)).toEqual("13th");
36+
expect(getOrdinalNumber(23)).toEqual("23rd");
37+
expect(getOrdinalNumber(143)).toEqual("143rd");
38+
});
39+
40+
// Case 4: All other numbers
41+
test("should append 'th' for all other numbers", () => {
42+
expect(getOrdinalNumber(4)).toEqual("4th");
43+
expect(getOrdinalNumber(11)).toEqual("11th");
2044
});
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count === 0) {
3+
return "";
4+
} else if (count > 0) {
5+
return str.repeat(count);
6+
} else {
7+
throw new Error("Count must be a non-negative integer");
8+
}
9+
return "";
310
}
411

512
module.exports = repeatStr;

Sprint-3/2-practice-tdd/repeat-str.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,32 @@ test("should repeat the string count times", () => {
2020
// Given a target string `str` and a `count` equal to 1,
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
23+
test("should return the original string when count is 1", () => {
24+
const str = "world";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
expect(repeatedStr).toEqual("world");
28+
});
2329

2430
// Case: Handle count of 0:
2531
// Given a target string `str` and a `count` equal to 0,
2632
// When the repeatStr function is called with these inputs,
2733
// Then it should return an empty string.
34+
test("should return an empty string when count is 0", () => {
35+
const str = "test";
36+
const count = 0;
37+
const repeatedStr = repeatStr(str, count);
38+
expect(repeatedStr).toEqual("");
39+
});
2840

2941
// Case: Handle negative count:
3042
// Given a target string `str` and a negative integer `count`,
3143
// When the repeatStr function is called with these inputs,
3244
// Then it should throw an error, as negative counts are not valid.
45+
test("should throw an error when count is negative", () => {
46+
const str = "error";
47+
const count = -2;
48+
expect(() => {
49+
repeatStr(str, count);
50+
}).toThrow("Count must be a non-negative integer");
51+
});

0 commit comments

Comments
 (0)