Skip to content

Commit d4e4a58

Browse files
committed
Sprint 3 Practice TDD
1 parent 3372770 commit d4e4a58

File tree

6 files changed

+103
-7
lines changed

6 files changed

+103
-7
lines changed

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

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

5-
module.exports = countChar;
13+
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 the character does not exist in the string", () => {
27+
const str = "hello";
28+
const char = "z";
29+
const count = countChar(str, char);
30+
expect(count).toEqual(0);
31+
});
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastTwoDigits = num % 100;
3+
const lastDigit = num % 10;
4+
5+
// Special case for 11, 12, 13
6+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
7+
return num + "th";
8+
}
9+
10+
if (lastDigit === 1) {
11+
return num + "st";
12+
}
13+
14+
if (lastDigit === 2) {
15+
return num + "nd";
16+
}
17+
18+
if (lastDigit === 3) {
19+
return num + "rd";
20+
}
21+
22+
return num + "th";
323
}
424

5-
module.exports = getOrdinalNumber;
25+
module.exports = getOrdinalNumber;

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,36 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
21+
22+
// Case 2: Numbers ending with 2 (but not 12)
23+
// When the number ends with 2, except those ending with 12,
24+
// Then the function should return a string by appending "nd" to the number.
25+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
26+
expect(getOrdinalNumber(2)).toEqual("2nd");
27+
expect(getOrdinalNumber(32)).toEqual("32nd");
28+
expect(getOrdinalNumber(102)).toEqual("102nd");
29+
});
30+
31+
// Case 3: Numbers ending with 3 (but not 13)
32+
// When the number ends with 3, except those ending with 13,
33+
// Then the function should return a string by appending "rd" to the number.
34+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
35+
expect(getOrdinalNumber(3)).toEqual("3rd");
36+
expect(getOrdinalNumber(43)).toEqual("43rd");
37+
expect(getOrdinalNumber(103)).toEqual("103rd");
38+
});
39+
40+
// Case 4: Numbers ending with 11, 12, or 13
41+
// These are special cases and should always end with "th".
42+
test("should append 'th' for numbers ending with 11, 12, or 13", () => {
43+
expect(getOrdinalNumber(11)).toEqual("11th");
44+
expect(getOrdinalNumber(12)).toEqual("12th");
45+
expect(getOrdinalNumber(13)).toEqual("13th");
46+
});
47+
48+
// Case 5: All other numbers should end with "th"
49+
test("should append 'th' for all other numbers", () => {
50+
expect(getOrdinalNumber(4)).toEqual("4th");
51+
expect(getOrdinalNumber(10)).toEqual("10th");
52+
expect(getOrdinalNumber(100)).toEqual("100th");
53+
});
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count cannot be negative");
4+
}
5+
6+
let result = "";
7+
8+
for (let i = 0; i < count; i++) {
9+
result += str;
10+
}
11+
12+
return result;
313
}
414

5-
module.exports = repeatStr;
15+
module.exports = repeatStr;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,30 @@ test("should repeat the string count times", () => {
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
2323

24+
test("should return the original string when count is 1", () => {
25+
const str = "hello";
26+
const count = 1;
27+
const repeatedStr = repeatStr(str, count);
28+
expect(repeatedStr).toEqual("hello");
29+
});
30+
2431
// Case: Handle count of 0:
2532
// Given a target string `str` and a `count` equal to 0,
2633
// When the repeatStr function is called with these inputs,
2734
// Then it should return an empty string.
2835

36+
test("should return an empty string when count is 0", () => {
37+
const str = "hello";
38+
const count = 0;
39+
const repeatedStr = repeatStr(str, count);
40+
expect(repeatedStr).toEqual("");
41+
});
42+
2943
// Case: Handle negative count:
3044
// Given a target string `str` and a negative integer `count`,
3145
// When the repeatStr function is called with these inputs,
3246
// Then it should throw an error, as negative counts are not valid.
47+
48+
test("should throw an error when count is negative", () => {
49+
expect(() => repeatStr("hello", -1)).toThrow();
50+
});

0 commit comments

Comments
 (0)