Skip to content

Commit f802832

Browse files
committed
practice tdd
1 parent b31a586 commit f802832

6 files changed

Lines changed: 73 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
return stringOfCharacters.split("").filter((char) => char === findCharacter)
3+
.length;
34
}
45

56
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 the character does not occur in the string", () => {
26+
const str = "hello";
27+
const char = "z";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
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+
if (num % 100 >= 11 && num % 100 <= 13) {
3+
return num + "th";
4+
}
5+
6+
if (num % 10 === 1) {
7+
return num + "st";
8+
}
9+
10+
if (num % 10 === 2) {
11+
return num + "nd";
12+
}
13+
14+
if (num % 10 === 3) {
15+
return num + "rd";
16+
}
17+
18+
return num + "th";
319
}
420

521
module.exports = getOrdinalNumber;

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,30 @@ 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+
// Case 2: Numbers ending with 2 (but not 12)
22+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
23+
expect(getOrdinalNumber(2)).toEqual("2nd");
24+
expect(getOrdinalNumber(22)).toEqual("22nd");
25+
expect(getOrdinalNumber(102)).toEqual("102nd");
26+
});
27+
28+
// Case 3: Numbers ending with 3 (but not 13)
29+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
30+
expect(getOrdinalNumber(3)).toEqual("3rd");
31+
expect(getOrdinalNumber(23)).toEqual("23rd");
32+
expect(getOrdinalNumber(103)).toEqual("103rd");
33+
});
34+
35+
// Case 4: Numbers ending with 11, 12 or 13
36+
test("should append 'th' for numbers ending with 11, 12 or 13", () => {
37+
expect(getOrdinalNumber(11)).toEqual("11th");
38+
expect(getOrdinalNumber(12)).toEqual("12th");
39+
expect(getOrdinalNumber(13)).toEqual("13th");
40+
});
41+
42+
// Case 5: All other numbers
43+
test("should append 'th' for all other numbers", () => {
44+
expect(getOrdinalNumber(4)).toEqual("4th");
45+
expect(getOrdinalNumber(20)).toEqual("20th");
46+
expect(getOrdinalNumber(100)).toEqual("100th");
47+
});
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
function repeatStr() {
1+
function repeatStr(str, count) {
22
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
33
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
4+
if (count < 0) {
5+
throw new Error("Invalid count");
6+
}
7+
8+
let result = "";
9+
10+
for (let i = 0; i < count; i++) {
11+
result += str;
12+
}
13+
14+
return result;
515
}
616

717
module.exports = repeatStr;

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,20 @@ 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 return the same string when count is 1", () => {
24+
expect(repeatStr("hello", 1)).toBe("hello");
25+
});
2426
// Case: Handle count of 0:
2527
// Given a target string `str` and a `count` equal to 0,
2628
// When the repeatStr function is called with these inputs,
2729
// Then it should return an empty string.
28-
30+
test("should return empty string when count is 0", () => {
31+
expect(repeatStr("hello", 0)).toBe("");
32+
});
2933
// Case: Handle negative count:
3034
// Given a target string `str` and a negative integer `count`,
3135
// When the repeatStr function is called with these inputs,
3236
// Then it should throw an error, as negative counts are not valid.
37+
test("should throw error when count is negative", () => {
38+
expect(() => repeatStr("hello", -1)).toThrow("Invalid count");
39+
});

0 commit comments

Comments
 (0)