Skip to content

Commit d6a54b8

Browse files
committed
2-practice-tdd completed
1 parent 3372770 commit d6a54b8

File tree

6 files changed

+176
-9
lines changed

6 files changed

+176
-9
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
if (findCharacter === "")
3+
return 0;
4+
else
5+
return stringOfCharacters.split(findCharacter).length -1;
36
}
47

5-
module.exports = countChar;
8+
module.exports = countChar;

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

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const countChar = require("./count");
44
// When the countChar function is called with these inputs,
55
// Then it should:
66

7-
// Scenario: Multiple Occurrences
7+
// Scenario 1: Multiple Occurrences
88
// Given the input string `str`,
99
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
1010
// When the function is called with these inputs,
@@ -17,8 +17,82 @@ test("should count multiple occurrences of a character", () => {
1717
expect(count).toEqual(5);
1818
});
1919

20-
// Scenario: No Occurrences
20+
// Scenario 2: No Occurrences
2121
// Given the input string `str`,
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 if there are no occurrences of a character", () => {
27+
const str = "there is no two number here";
28+
const char = "2";
29+
const count = countChar(str, char);
30+
expect(count).toEqual(0);
31+
});
32+
33+
34+
// Scenario 3: Single Occurrence
35+
// Given the input string `str`,
36+
// And a character `char` that exists only once within `str`.
37+
// When the function is called with these inputs,
38+
// Then it should return 1, indicating that single occurrence of `char` were found.
39+
40+
test("should return 1 if there is single occurrence of a character", () => {
41+
const str = " let's find if we got what you are looking for";
42+
const char = "'";
43+
const count = countChar(str, char);
44+
expect(count).toEqual(1);
45+
});
46+
47+
// Scenario 4: Zero occurrence when the string is empty
48+
// Given the input string `str` which is empty,
49+
// And a character `char` that can't be existed in the `str` as it is empty.
50+
// When the function is called with these inputs,
51+
// Then it should return 0, indicating that empty string can't hold that char.
52+
53+
test("should return 0 as the string is empty", () => {
54+
const str = "";
55+
const char = "2";
56+
const count = countChar(str, char);
57+
expect(count).toEqual(0);
58+
});
59+
60+
// Scenario 5: Multiple occurrences if the string contains single empty spaces and our char is an empty space
61+
// Given the input string `str` that contains ' ' empty spaces,
62+
// And a character `char` which is just single empty space ' '.
63+
// When the function is called with these inputs,
64+
// Then it should return the number of occurrences of single empty spaces in the string.
65+
66+
test("should return multiple occurrences of empty spaces in the string", () => {
67+
const str = "Hi I have got ten single empty spaces in this string.";
68+
const char = " ";
69+
const count = countChar(str, char);
70+
expect(count).toEqual(10);
71+
});
72+
73+
74+
// Scenario 6: No occurrences if the char is just an empty char
75+
// Given the input string `str` that contains any number of characters or no characters at all,
76+
// And a character `char` which is empty ''.
77+
// When the function is called with these inputs,
78+
// Then it should return 0 as there is nothing to count as the char is just empty.
79+
80+
test("should return 0 as the char is just empty", () => {
81+
const str = "Hi I am a string that can be empty or not empty but I can't count empty characters.";
82+
const char = "";
83+
const count = countChar(str, char);
84+
expect(count).toEqual(0);
85+
});
86+
87+
// Scenario 7: Multiple Occurrences at different places
88+
// Given the input string `str`,
89+
// And a character `char` that occurs more than one times in `str` (e.g., 'a' in 'add the angle to the antenna'),
90+
// When the function is called with these inputs,
91+
// Then it should correctly count occurrences of `char` a in the string.
92+
93+
test("should count multiple occurrences of a character", () => {
94+
const str = "add the angle to the antenna";
95+
const char = "a";
96+
const count = countChar(str, char);
97+
expect(count).toEqual(4);
98+
});
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
let numberString = String(num);
3+
lastTwoCharactersNumberString = numberString.slice(-2);
4+
lastCharacterNumberString = numberString.slice(-1);
5+
if(lastTwoCharactersNumberString === "11")
6+
return num+ "th";
7+
else if(lastCharacterNumberString === "1")
8+
return num+ "st";
9+
else if(lastCharacterNumberString === "2")
10+
return num+"nd";
11+
else if(lastCharacterNumberString === "3")
12+
return num + "rd";
13+
else
14+
return num + "th";
315
}
416

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

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,52 @@ 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");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
20+
expect(getOrdinalNumber(-171)).toEqual("-171st");
2021
});
22+
23+
// Case 2: Special case for number 11)
24+
// When the number is 11 or the numbers ends with 11,
25+
// Then the function should return a string by appending "th" to the number.
26+
test("should append 'th' for number 11", () => {
27+
expect(getOrdinalNumber(11)).toEqual("11th");
28+
expect(getOrdinalNumber(111)).toEqual("111th");
29+
expect(getOrdinalNumber(12211)).toEqual("12211th");
30+
expect(getOrdinalNumber(98011)).toEqual("98011th");
31+
expect(getOrdinalNumber(-87611)).toEqual("-87611th");
32+
});
33+
34+
// Case 3: Numbers ending with 2
35+
// When the number is ending with 2,
36+
// Then the function should return a string by appending "nd" to the number.
37+
test("should append 'st' for numbers ending with 2", () => {
38+
expect(getOrdinalNumber(2)).toEqual("2nd");
39+
expect(getOrdinalNumber(22)).toEqual("22nd");
40+
expect(getOrdinalNumber(152)).toEqual("152nd");
41+
expect(getOrdinalNumber(-82)).toEqual("-82nd");
42+
});
43+
44+
45+
// Case 4: Numbers ending with 3
46+
// When the number is ending with 3,
47+
// Then the function should return a string by appending "rd" to the number.
48+
test("should append 'st' for numbers ending with 3", () => {
49+
expect(getOrdinalNumber(3)).toEqual("3rd");
50+
expect(getOrdinalNumber(33)).toEqual("33rd");
51+
expect(getOrdinalNumber(183)).toEqual("183rd");
52+
expect(getOrdinalNumber(-903)).toEqual("-903rd");
53+
});
54+
55+
// Case 5: Numbers not ending 1, 2 and 3 except 11
56+
// When the number is not ending with 1, 2 and 3 except 11,
57+
// Then the function should return a string by appending "th" to the number.
58+
test("should append 'th' for numbers not ending with 1, 2 and 3 except those ending with 11", () => {
59+
expect(getOrdinalNumber(4)).toEqual("4th");
60+
expect(getOrdinalNumber(10)).toEqual("10th");
61+
expect(getOrdinalNumber(0)).toEqual("0th");
62+
expect(getOrdinalNumber(125)).toEqual("125th");
63+
expect(getOrdinalNumber(1000)).toEqual("1000th");
64+
expect(getOrdinalNumber(87939)).toEqual("87939th");
65+
expect(getOrdinalNumber(-780987)).toEqual("-780987th");
66+
67+
});
68+
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
try{
3+
if(count === 0)
4+
return "";
5+
else if(count === 1)
6+
return str;
7+
else if(count < 0)
8+
throw new Error("an error is thrown");
9+
else
10+
return str.repeat(count);
11+
}
12+
catch(e){
13+
return e.message;
14+
}
315
}
416

5-
module.exports = repeatStr;
17+
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
@@ -20,13 +20,31 @@ 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 repeat the original string as the count is 1", () => {
24+
const str = "OneWord";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
expect(repeatedStr).toEqual("OneWord");
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 as the 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 repeat the string count times", () => {
46+
const str = "hello";
47+
const count = -2;
48+
const repeatedStr = repeatStr(str, count);
49+
expect(repeatedStr).toEqual("an error is thrown");
50+
});

0 commit comments

Comments
 (0)