Skip to content

Commit 5deee0a

Browse files
committed
Implement practice TDD exercises
1 parent 06520c4 commit 5deee0a

File tree

6 files changed

+85
-6
lines changed

6 files changed

+85
-6
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+
const str = stringOfCharacters.length;
4+
5+
for (let i = 0; i < str; i++) {
6+
if (stringOfCharacters[i] === findCharacter) {
7+
count += 1;
8+
console.log(stringOfCharacters[i]);
9+
}
10+
}
11+
return count;
312
}
13+
console.log(countChar("little", "t"));
414

515
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("no occurrences of character in string", () => {
27+
const str = "little"
28+
const char = "b"
29+
const count = countChar(str, char);
30+
expect(count).toEqual(0);
31+
});
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastTwoDigits = num % 100;
3+
const lastDigit = num % 10;
4+
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
6+
return `${num}${"th"}`;
7+
}
8+
9+
switch (lastDigit) {
10+
case 1:
11+
return `${num}${"st"}`;
12+
case 2:
13+
return `${num}${"nd"}`;
14+
case 3:
15+
return `${num}${"rd"}`;
16+
default:
17+
return `${num}${"th"}`;
18+
}
319
}
420

21+
console.log(getOrdinalNumber(3));
22+
523
module.exports = getOrdinalNumber;

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,29 @@ 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 in 2 → add nd → (2nd, 22nd, 42nd)
23+
test("should append 'nd' for numbers ending with 2", () => {
24+
expect(getOrdinalNumber(2)).toEqual("2nd");
25+
expect(getOrdinalNumber(22)).toEqual("22nd");
26+
expect(getOrdinalNumber(142)).toEqual("142nd");
27+
});
28+
// Case 3: Numbers ending in 3 → add rd → (3rd, 23rd, 53rd)
29+
test("should append '3rd' for numbers ending with 3", () => {
30+
expect(getOrdinalNumber(3)).toEqual("3rd");
31+
expect(getOrdinalNumber(33)).toEqual("33rd");
32+
expect(getOrdinalNumber(153)).toEqual("153rd");
33+
});
34+
// Case 4: All other numbers → add th → (4th, 6th, 20th, 100th)
35+
test("should append 'th' for numbers ending with 4", () => {
36+
expect(getOrdinalNumber(4)).toEqual("4th");
37+
expect(getOrdinalNumber(20)).toEqual("20th");
38+
expect(getOrdinalNumber(100)).toEqual("100th");
39+
});
40+
// Exceptions: Numbers ending in 11, 12, and 13 use -th (e.g., 11th, 12th, 13th).
41+
test("should append 'th' for numbers ending with 11, 12 and 13", () => {
42+
expect(getOrdinalNumber(11)).toEqual("11th");
43+
expect(getOrdinalNumber(12)).toEqual("12th");
44+
expect(getOrdinalNumber(113)).toEqual("113th");
45+
});
46+
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if(count < 0){
3+
throw new Error("negative counts are not valid");
4+
}
5+
return str.repeat(count);
36
}
47

58
module.exports = repeatStr;

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,28 @@ 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-
23+
test("should repeat the string count times", () => {
24+
const str = "hello";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
expect(repeatedStr).toEqual("hello");
28+
});
2429
// Case: Handle count of 0:
2530
// Given a target string `str` and a `count` equal to 0,
2631
// When the repeatStr function is called with these inputs,
2732
// Then it should return an empty string.
28-
33+
test("should repeat the string count times", () => {
34+
const str = "hello";
35+
const count = 0;
36+
const repeatedStr = repeatStr(str, count);
37+
expect(repeatedStr).toEqual("");
38+
});
2939
// Case: Handle negative count:
3040
// Given a target string `str` and a negative integer `count`,
3141
// When the repeatStr function is called with these inputs,
3242
// Then it should throw an error, as negative counts are not valid.
43+
test(`Should throw an error`, () => {
44+
expect(() => {
45+
repeatStr("hello", -1);
46+
}).toThrow();
47+
});

0 commit comments

Comments
 (0)