Skip to content

Commit d02bb17

Browse files
feat: restore TDD work
1 parent 3372770 commit d02bb17

File tree

6 files changed

+116
-7
lines changed

6 files changed

+116
-7
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let count =0;
3+
for(let i=0; i<stringOfCharacters.length; i++){
4+
if(stringOfCharacters[i] === findCharacter){
5+
count=count+1;}}
6+
return count;
37
}
4-
8+
console.log(countChar("banana","a"))//3
59
module.exports = countChar;

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// implement a function countChar that counts the number of times a character occurs in a string
22
const countChar = require("./count");
3+
34
// Given a string `str` and a single character `char` to search for,
45
// When the countChar function is called with these inputs,
56
// Then it should:
@@ -22,3 +23,32 @@ test("should count multiple occurrences of a character", () => {
2223
// And a character `char` that does not exist within `str`.
2324
// When the function is called with these inputs,
2425
// Then it should return 0, indicating that no occurrences of `char` were found.
26+
27+
test("should return 0 when character does not exist in string", () => {
28+
const str = "welcome";
29+
const char = "z";
30+
const count = countChar(str, char);
31+
expect(count).toEqual(0);
32+
});
33+
34+
// Scenario: Single Occurrence
35+
// Given a string where the character appears exactly once,
36+
// Then it should return 1.
37+
38+
test("should return 1 when character appears once", () => {
39+
const str = "apple";
40+
const char = "a";
41+
const count = countChar(str, char);
42+
expect(count).toEqual(1);
43+
});
44+
45+
// Scenario: Empty String
46+
// Given an empty string,
47+
// Then it should return 0 for any character.
48+
49+
test("should return 0 for empty string", () => {
50+
const str = "";
51+
const char = "a";
52+
const count = countChar(str, char);
53+
expect(count).toEqual(0);
54+
});
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
12
function getOrdinalNumber(num) {
2-
return "1st";
3+
const lastTwoDigits = num % 100;
4+
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
6+
return num + "th";
7+
}
8+
9+
const lastDigit = num % 10;
10+
11+
if (lastDigit === 1) return num + "st";
12+
if (lastDigit === 2) return num + "nd";
13+
if (lastDigit === 3) return num + "rd";
14+
15+
return num + "th";
316
}
417

518
module.exports = getOrdinalNumber;

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,44 @@ 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+
22+
// Case 2: Numbers ending with 2 (but not 12)
23+
// When the number ends with 2, except those ending with 12,
24+
// Then the function should return a string by appending "nd" to the number.
25+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
26+
expect(getOrdinalNumber(2)).toEqual("2nd");
27+
expect(getOrdinalNumber(22)).toEqual("22nd");
28+
expect(getOrdinalNumber(102)).toEqual("102nd");
29+
});
30+
31+
// Case 3: Numbers ending with 3 (but not 13)
32+
// When the number ends with 3, except those ending with 13,
33+
// Then the function should return a string by appending "rd" to the number.
34+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
35+
expect(getOrdinalNumber(3)).toEqual("3rd");
36+
expect(getOrdinalNumber(23)).toEqual("23rd");
37+
expect(getOrdinalNumber(203)).toEqual("203rd");
38+
});
39+
40+
// Case 4: Numbers ending with 11, 12, 13
41+
// When the number ends with 11, 12, or 13,
42+
// Then the function should return a string by appending "th" to the number.
43+
test("should append 'th' for numbers ending with 11, 12, or 13", () => {
44+
expect(getOrdinalNumber(11)).toEqual("11th");
45+
expect(getOrdinalNumber(12)).toEqual("12th");
46+
expect(getOrdinalNumber(13)).toEqual("13th");
47+
expect(getOrdinalNumber(111)).toEqual("111th");
48+
expect(getOrdinalNumber(112)).toEqual("112th");
49+
expect(getOrdinalNumber(113)).toEqual("113th");
50+
});
51+
52+
// Case 5: All other numbers
53+
// When the number does not end with 1, 2, 3 (or ends with 11, 12, 13),
54+
// Then the function should return a string by appending "th" to the number.
55+
test("should append 'th' for all other numbers", () => {
56+
expect(getOrdinalNumber(4)).toEqual("4th");
57+
expect(getOrdinalNumber(10)).toEqual("10th");
58+
expect(getOrdinalNumber(14)).toEqual("14th");
59+
expect(getOrdinalNumber(20)).toEqual("20th");
60+
expect(getOrdinalNumber(100)).toEqual("100th");
61+
});
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
function repeatStr() {
2-
return "hellohellohello";
3-
}
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count cannot be negative");
4+
}
45

6+
return str.repeat(count);
7+
}
58
module.exports = repeatStr;

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Implement a function repeatStr
22
const repeatStr = require("./repeat-str");
3+
34
// Given a target string `str` and a positive integer `count`,
45
// When the repeatStr function is called with these inputs,
56
// Then it should:
@@ -8,7 +9,6 @@ const repeatStr = require("./repeat-str");
89
// Given a target string `str` and a positive integer `count` greater than 1,
910
// When the repeatStr function is called with these inputs,
1011
// Then it should return a string that contains the original `str` repeated `count` times.
11-
1212
test("should repeat the string count times", () => {
1313
const str = "hello";
1414
const count = 3;
@@ -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 return 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 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 error when count is negative", () => {
46+
const str = "hello";
47+
const count = -2;
48+
49+
expect(() => repeatStr(str, count)).toThrow();
50+
});

0 commit comments

Comments
 (0)