Skip to content

Commit 0967a9e

Browse files
committed
refactor: update three function and add tests for three files
1 parent 4455f27 commit 0967a9e

File tree

6 files changed

+3442
-6
lines changed

6 files changed

+3442
-6
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
1+
function countChar(stringOfCharacters, findChar) {
2+
let count = 0;
3+
for (const char of stringOfCharacters) {
4+
if (char === findChar) {
5+
count++;
6+
}
7+
}
8+
return count;
39
}
410

511
module.exports = countChar;
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
let last2Digit = num % 100;
3+
let las1Digit = num % 10;
4+
if (last2Digit >= 11 && last2Digit <= 13) {
5+
return num + "th";
6+
}
7+
if (las1Digit === 1) {
8+
return num + "st";
9+
} else if (las1Digit === 2) {
10+
return num + "nd";
11+
} else if (las1Digit === 3) {
12+
return num + "rd";
13+
} else {
14+
return num + "th";
15+
}
316
}
417

518
module.exports = getOrdinalNumber;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,17 @@ const getOrdinalNumber = require("./get-ordinal-number");
1111
test("should return '1st' for 1", () => {
1212
expect(getOrdinalNumber(1)).toEqual("1st");
1313
});
14+
test("should return '22nd' for 22", () => {
15+
expect(getOrdinalNumber(22)).toEqual("22nd");
16+
});
17+
18+
test("should return '3th' for 3", () => {
19+
expect(getOrdinalNumber(3)).toEqual("3rd");
20+
});
21+
test("should return '23rd' for 23", () => {
22+
expect(getOrdinalNumber(23)).toEqual("23rd");
23+
});
24+
25+
test("should return '100th' for 100", () => {
26+
expect(getOrdinalNumber(100)).toEqual("100th");
27+
});

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
function repeat() {
2-
return "hellohellohello";
1+
function repeat(str, count) {
2+
if (count < 0) {
3+
return "Count must be non-negative";
4+
}
5+
return str.repeat(count);
36
}
4-
57
module.exports = repeat;

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,31 @@ test("should repeat the string count times", () => {
2121
// When the repeat function is called with these inputs,
2222
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
2323

24+
test("should return the original str without repetition", () => {
25+
const str = "shreef";
26+
const count = 1;
27+
const repeatedStr = repeat(str, count);
28+
expect(repeatedStr).toEqual("shreef");
29+
});
30+
2431
// case: Handle Count of 0:
2532
// Given a target string str and a count equal to 0,
2633
// When the repeat function is called with these inputs,
2734
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
35+
test("should return an empty string", () => {
36+
const str = "ahmed";
37+
const count = 0;
38+
const repeatedStr = repeat(str, count);
39+
expect(repeatedStr).toEqual("");
40+
});
2841

2942
// case: Negative Count:
3043
// Given a target string str and a negative integer count,
3144
// When the repeat function is called with these inputs,
3245
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
46+
test("should throw an error or return an appropriate error message", () => {
47+
const str = "jone";
48+
const count = -5;
49+
const repeatedStr = repeat(str, count);
50+
expect(repeatedStr).toBe("Count must be non-negative");
51+
});

0 commit comments

Comments
 (0)