Skip to content

Commit e346573

Browse files
committed
Completed Practice-tdd tasks
1 parent 3372770 commit e346573

File tree

6 files changed

+49
-2
lines changed

6 files changed

+49
-2
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,21 @@ 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 occur in the string", () => {
26+
const str = "hello world";
27+
const char = "x";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
31+
32+
// Scenario: Case Sensitivity
33+
// Given the input string `str`,
34+
// And a character `char` that occurs in `str` but with different cases (e.g., 'a' and 'A'),
35+
// When the function is called with these inputs,
36+
// Then it should count only the occurrences of `char` that match the case provided in the input.
37+
test("should count only occurrences of the character that match the case", () => {
38+
const str = "AaAaA";
39+
const char = "a";
40+
const count = countChar(str, char);
41+
expect(count).toEqual(2);
42+
});
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+
const lastDigit = num % 10;
3+
const lastTwoDigits = num % 100;
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

521
module.exports = getOrdinalNumber;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ 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+

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ function repeatStr() {
33
}
44

55
module.exports = repeatStr;
6+
7+

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ test("should repeat the string count times", () => {
3030
// Given a target string `str` and a negative integer `count`,
3131
// When the repeatStr function is called with these inputs,
3232
// Then it should throw an error, as negative counts are not valid.
33+
34+

0 commit comments

Comments
 (0)