Skip to content

Commit 9ffb265

Browse files
committed
I have completed the pratice-tdd
1 parent 3a52de2 commit 9ffb265

File tree

6 files changed

+77
-6
lines changed

6 files changed

+77
-6
lines changed

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

Lines changed: 9 additions & 1 deletion
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

513
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 `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 character is not found ", () => {
26+
const str = "helo world";
27+
const char = "z";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastTwoDigits =num % 100;
3+
4+
// Special cases for 11, 12, and 13
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
6+
return num + "th";
7+
8+
}
9+
const lastDigit = num % 10;
10+
if (lastDigit === 1) return num + "st";
11+
if (lastDigit ===2) return num + "nd";
12+
if (lastDigit ===3) return num + "rd";
13+
return num + "th";
14+
315
}
416

517
module.exports = getOrdinalNumber;

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,33 @@ 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(131)).toEqual("131st");
20-
});
19+
expect(getOrdinalNumber(41)).toEqual("41st");
20+
});
21+
22+
// Case 2: Numbers ending with 2 (but not 12)
23+
test("should append 'nd' for number ending with 2 except those ending with 12", () => {
24+
expect(getOrdinalNumber(2)).toEqual("2nd");
25+
expect(getOrdinalNumber(22)).toEqual("22nd");
26+
expect(getOrdinalNumber(42)).toEqual("42nd");
27+
});
28+
29+
// Case 3: Numbers ending with 3 (but not 13)
30+
test("should append 'rd' for number anding with 3 expect those ending with 13", () => {
31+
expect(getOrdinalNumber(3)).toEqual("3rd");
32+
expect(getOrdinalNumber(33)).toEqual("33rd");
33+
expect(getOrdinalNumber(43)).toEqual("43rd");
34+
});
35+
36+
// Case 4: Number ending with 4 (but not 14)
37+
test("should append 'th' for number ending with 4 expcet those ending with 14", () => {
38+
expect(getOrdinalNumber(4)).toEqual("4th");
39+
expect(getOrdinalNumber(44)).toEqual("44th");
40+
expect(getOrdinalNumber(54)).toEqual("54th");
41+
});
42+
43+
// Case 5: Number ending with 5 (but not 15)
44+
test("should append 'th' for number ending with 5 except those ending with 15", () => {
45+
expect(getOrdinalNumber(5)).toEqual("5th");
46+
expect(getOrdinalNumber(55)).toEqual("55th");
47+
expect(getOrdinalNumber(65)).toEqual("65th");
48+
});
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(n, str) {
2+
return str.repeat(n);
33
}
44

55
module.exports = repeatStr;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,30 @@ 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 = "hello";
25+
const count = 1;
26+
const repeatedStr = repeatStr(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 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 = "hello";
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 = "hello";
47+
const count = -1;
48+
expect(() => repeatStr(str, count)).toThrow();
49+
});

0 commit comments

Comments
 (0)