Skip to content

Commit 35b6637

Browse files
committed
Merge branch 'coursework/sprint-3-practice-tdd' into coursework/sprint-3-implement-and-rewrite/implement/1-get-angle-type
2 parents 56625ec + 00e6c6d commit 35b6637

File tree

6 files changed

+166
-5
lines changed

6 files changed

+166
-5
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let count = 0;
3+
4+
// Loop through each character in the string
5+
for (let i = 0; i < stringOfCharacters.length; i++) {
6+
// If the current character matches the character we're looking for
7+
if (stringOfCharacters[i] === findCharacter) {
8+
count++;
9+
}
10+
}
11+
12+
return count;
313
}
414

515
module.exports = countChar;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character char that does not exist within the case-sensitive str,
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
25+
test("should return 0 for no occurrences", () => {
26+
const str = "hello";
27+
const char = "x";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
// Handle special cases for teens (11th, 12th, 13th)
3+
if (num % 100 >= 11 && num % 100 <= 13) {
4+
return num + "th";
5+
}
6+
7+
// Handle based on last digit
8+
const lastDigit = num % 10;
9+
10+
if (lastDigit === 1) {
11+
return num + "st";
12+
} else if (lastDigit === 2) {
13+
return num + "nd";
14+
} else if (lastDigit === 3) {
15+
return num + "rd";
16+
} else {
17+
return num + "th";
18+
}
319
}
420

521
module.exports = getOrdinalNumber;

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

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,100 @@ const getOrdinalNumber = require("./get-ordinal-number");
1111
test("should return '1st' for 1", () => {
1212
expect(getOrdinalNumber(1)).toEqual("1st");
1313
});
14+
15+
// Case 2: Identify the ordinal number for 2
16+
// When the number is 2,
17+
// Then the function should return "2nd"
18+
19+
test("should return '2nd' for 2", () => {
20+
expect(getOrdinalNumber(2)).toEqual("2nd");
21+
});
22+
23+
// Case 3: Identify the ordinal number for 3
24+
// When the number is 3,
25+
// Then the function should return "3rd"
26+
27+
test("should return '3rd' for 3", () => {
28+
expect(getOrdinalNumber(3)).toEqual("3rd");
29+
});
30+
31+
// Case 4: Identify the ordinal number for 4
32+
// When the number is 4,
33+
// Then the function should return "4th"
34+
35+
test("should return '4th' for 4", () => {
36+
expect(getOrdinalNumber(4)).toEqual("4th");
37+
});
38+
39+
// Case 5: Identify the ordinal number for 11
40+
// When the number is 11,
41+
// Then the function should return "11th"
42+
43+
test("should return '11th' for 11", () => {
44+
expect(getOrdinalNumber(11)).toEqual("11th");
45+
});
46+
47+
// Case 6: Identify the ordinal number for 12
48+
// When the number is 12,
49+
// Then the function should return "12th"
50+
51+
test("should return '12th' for 12", () => {
52+
expect(getOrdinalNumber(12)).toEqual("12th");
53+
});
54+
55+
// Case 7: Identify the ordinal number for 13
56+
// When the number is 13,
57+
// Then the function should return "13th"
58+
59+
test("should return '13th' for 13", () => {
60+
expect(getOrdinalNumber(13)).toEqual("13th");
61+
});
62+
63+
// Case 8: Identify the ordinal number for 21
64+
// When the number is 21,
65+
// Then the function should return "21st"
66+
67+
test("should return '21st' for 21", () => {
68+
expect(getOrdinalNumber(21)).toEqual("21st");
69+
});
70+
71+
// Case 9: Identify the ordinal number for 22
72+
// When the number is 22,
73+
// Then the function should return "22nd"
74+
75+
test("should return '22nd' for 22", () => {
76+
expect(getOrdinalNumber(22)).toEqual("22nd");
77+
});
78+
79+
// Case 10: Identify the ordinal number for 23
80+
// When the number is 23,
81+
// Then the function should return "23rd"
82+
83+
test("should return '23rd' for 23", () => {
84+
expect(getOrdinalNumber(23)).toEqual("23rd");
85+
});
86+
87+
// Case 11: Identify the ordinal number for 101
88+
// When the number is 101,
89+
// Then the function should return "101st"
90+
91+
test("should return '101st' for 101", () => {
92+
expect(getOrdinalNumber(101)).toEqual("101st");
93+
});
94+
95+
// Case 12: Identify the ordinal number for 111
96+
// When the number is 111,
97+
// Then the function should return "111th"
98+
99+
test("should return '111th' for 111", () => {
100+
expect(getOrdinalNumber(111)).toEqual("111th");
101+
});
102+
103+
// Case 13: Identify the ordinal number for 0
104+
// When the number is 0,
105+
// Then the function should return "0th"
106+
107+
test("should return '0th' for 0", () => {
108+
expect(getOrdinalNumber(0)).toEqual("0th");
109+
});
110+

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
function repeat() {
2-
return "hellohellohello";
1+
function repeat(str, count) {
2+
// Handle edge cases
3+
if (count < 0) {
4+
throw new Error("Count cannot be negative");
5+
}
6+
7+
if (count === 0) {
8+
return "";
9+
}
10+
11+
// Build the repeated string
12+
let result = "";
13+
for (let i = 0; i < count; i++) {
14+
result += str;
15+
}
16+
17+
return result;
318
}
419

520
module.exports = repeat;

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,31 @@ test("should repeat the string count times", () => {
1919
// case: handle Count of 1:
2020
// Given a target string str and a count equal to 1,
2121
// When the repeat function is called with these inputs,
22-
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
22+
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition
23+
test("should return the original string for count of 1", () => {
24+
const str = "hello";
25+
const count = 1;
26+
const repeatedStr = repeat(str, count);
27+
expect(repeatedStr).toEqual("hello");
28+
});
2329

2430
// case: Handle Count of 0:
2531
// Given a target string str and a count equal to 0,
2632
// When the repeat function is called with these inputs,
2733
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
34+
test("should return an empty string for count of 0", () => {
35+
const str = "hello";
36+
const count = 0;
37+
const repeatedStr = repeat(str, count);
38+
expect(repeatedStr).toEqual("");
39+
});
2840

2941
// case: Negative Count:
3042
// Given a target string str and a negative integer count,
3143
// When the repeat function is called with these inputs,
3244
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
45+
test("should throw an error for negative count", () => {
46+
const str = "hello";
47+
const count = -2;
48+
expect(() => repeat(str, count)).toThrow("Count cannot be negative");
49+
});

0 commit comments

Comments
 (0)