Skip to content

Commit 218cead

Browse files
committed
Repeat of tdd
1 parent 0e99abc commit 218cead

6 files changed

Lines changed: 161 additions & 9 deletions

File tree

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let count = 0;
3+
4+
for (const letter of stringOfCharacters) {
5+
if (letter === findCharacter) {
6+
count++;
7+
}
8+
}
9+
10+
return count;
11+
312
}
413

514
module.exports = countChar;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,17 @@ 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+
test("should return 0 when the character does not exist in the string", () => {
26+
const str = "hello";
27+
const char = "z";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
31+
32+
// Bonus Scenario: Mixed Case (Character searching is case-sensitive)
33+
test("should be case-sensitive", () => {
34+
const str = "Abba";
35+
const char = "a";
36+
const count = countChar(str, char);
37+
expect(count).toEqual(1); // Only the lowercase 'a' is counted
38+
});
Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
1+
2+
/*
13
function getOrdinalNumber(num) {
2-
return "1st";
4+
const s = num.toString();
5+
const lastDigit = num % 10;
6+
const lastTwoDigits = num % 100;
7+
8+
// Rule for 11th, 12th, 13th (The "Teens" exception)
9+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
10+
return s + "th";
11+
//return "1st";
12+
13+
// Standard rules based on the last digit
14+
switch (lastDigit) {
15+
case 1:
16+
return s + "st";
17+
case 2:
18+
return s + "nd";
19+
case 3:
20+
return s + "rd";
21+
default:
22+
return s + "th";
23+
}
24+
}
325
}
26+
*/
27+
28+
29+
function getOrdinalNumber(num) {
30+
const s = num.toString();
31+
const lastDigit = num % 10;
32+
const lastTwoDigits = num % 100;
33+
34+
// Rule for 11th, 12th, 13th (The "Teens" exception)
35+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
36+
return s + "th";
37+
}
38+
39+
// Standard rules based on the last digit
40+
switch (lastDigit) {
41+
case 1:
42+
return s + "st";
43+
case 2:
44+
return s + "nd";
45+
case 3:
46+
return s + "rd";
47+
default:
48+
return s + "th";
49+
}
50+
}
51+
52+
453

554
module.exports = getOrdinalNumber;

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

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,42 @@ const getOrdinalNumber = require("./get-ordinal-number");
1313
// Case 1: Numbers ending with 1 (but not 11)
1414
// When the number ends with 1, except those ending with 11,
1515
// Then the function should return a string by appending "st" to the number.
16-
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
17-
expect(getOrdinalNumber(1)).toEqual("1st");
18-
expect(getOrdinalNumber(21)).toEqual("21st");
19-
expect(getOrdinalNumber(131)).toEqual("131st");
20-
});
16+
17+
describe("getOrdinalNumber", () => {
18+
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
19+
expect(getOrdinalNumber(1)).toEqual("1st");
20+
expect(getOrdinalNumber(21)).toEqual("21st");
21+
expect(getOrdinalNumber(131)).toEqual("131st");
22+
});
23+
24+
// Case 2: Numbers ending with 2 (but not 12)
25+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
26+
expect(getOrdinalNumber(2)).toBe("2nd");
27+
expect(getOrdinalNumber(42)).toBe("42nd");
28+
expect(getOrdinalNumber(122)).toBe("122nd");
29+
});
30+
31+
// Case 3: Numbers ending with 3 (but not 13)
32+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
33+
expect(getOrdinalNumber(3)).toBe("3rd");
34+
expect(getOrdinalNumber(53)).toBe("53rd");
35+
expect(getOrdinalNumber(1003)).toBe("1003rd");
36+
});
37+
38+
// Case 4: The Teen Exceptions (11, 12, 13)
39+
test("should append 'th' for numbers ending in 11, 12, or 13", () => {
40+
expect(getOrdinalNumber(11)).toBe("11th");
41+
expect(getOrdinalNumber(12)).toBe("12th");
42+
expect(getOrdinalNumber(13)).toBe("13th");
43+
expect(getOrdinalNumber(111)).toBe("111th"); // Edge case: triple digits
44+
});
45+
46+
// Case 5: All other numbers (Ending in 0, 4, 5, 6, 7, 8, 9)
47+
test("should append 'th' for all other numbers", () => {
48+
expect(getOrdinalNumber(0)).toBe("0th");
49+
expect(getOrdinalNumber(4)).toBe("4th");
50+
expect(getOrdinalNumber(9)).toBe("9th");
51+
expect(getOrdinalNumber(10)).toBe("10th");
52+
expect(getOrdinalNumber(20)).toBe("20th");
53+
});
54+
});
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+
throw new Error("Count must be a non-negative integer");
4+
}
5+
6+
// If count is 0, .repeat(0) naturally returns an empty string "".
7+
// If count is 1, .repeat(1) naturally returns the original string.
8+
return str.repeat(count);
9+
//return "hellohellohello";
310
}
411

512
module.exports = repeatStr;

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ const repeatStr = require("./repeat-str");
99
// When the repeatStr function is called with these inputs,
1010
// Then it should return a string that contains the original `str` repeated `count` times.
1111

12+
/*
1213
test("should repeat the string count times", () => {
1314
const str = "hello";
1415
const count = 3;
1516
const repeatedStr = repeatStr(str, count);
1617
expect(repeatedStr).toEqual("hellohellohello");
1718
});
19+
*/
1820

1921
// Case: handle count of 1:
2022
// Given a target string `str` and a `count` equal to 1,
@@ -30,3 +32,40 @@ test("should repeat the string count times", () => {
3032
// Given a target string `str` and a negative integer `count`,
3133
// When the repeatStr function is called with these inputs,
3234
// Then it should throw an error, as negative counts are not valid.
35+
36+
37+
describe("repeatStr", () => {
38+
// Case: handle multiple repetitions
39+
test("should repeat the string count times", () => {
40+
const str = "hello";
41+
const count = 3;
42+
const repeatedStr = repeatStr(str, count);
43+
expect(repeatedStr).toEqual("hellohellohello");
44+
});
45+
46+
// Case: handle count of 1
47+
test("should return the original string when count is 1", () => {
48+
const str = "apple";
49+
const count = 1;
50+
expect(repeatStr(str, count)).toEqual("apple");
51+
});
52+
53+
// Case: Handle count of 0
54+
test("should return an empty string when count is 0", () => {
55+
const str = "ghost";
56+
const count = 0;
57+
expect(repeatStr(str, count)).toEqual("");
58+
});
59+
60+
// Case: Handle negative count
61+
test("should throw an error when count is negative", () => {
62+
const str = "error";
63+
const count = -1;
64+
65+
// We wrap the call in a function so Jest can catch the error
66+
expect(() => {
67+
repeatStr(str, count);
68+
}).toThrow("Count must be a non-negative integer");
69+
});
70+
});
71+

0 commit comments

Comments
 (0)