Skip to content

Commit a75974d

Browse files
completed_Practice_TDD
1 parent 124ae45 commit a75974d

File tree

7 files changed

+81
-7
lines changed

7 files changed

+81
-7
lines changed

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

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

510
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
@@ -16,6 +16,12 @@ test("should count multiple occurrences of a character", () => {
1616
const count = countChar(str, char);
1717
expect(count).toEqual(5);
1818
});
19+
test("No Occurrences", () => {
20+
const str = "qwerty";
21+
const char = "k";
22+
const count = countChar(str, char);
23+
expect(count).toEqual(0);
24+
})
1925

2026
// Scenario: No Occurrences
2127
// Given the input string `str`,
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
let last2digit = num % 100
3+
let lastDigit = num % 10
4+
if (last2digit === 11 || last2digit === 12 || last2digit === 13) {
5+
return `${num}th`
6+
}
7+
else if (lastDigit === 1) {
8+
return `${num}st`
9+
}
10+
else if (lastDigit === 2) {
11+
return `${num}nd`
12+
}
13+
else if (lastDigit === 3) {
14+
return `${num}rd`
15+
}
16+
else return `${num}th`
317
}
418

519
module.exports = getOrdinalNumber;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,32 @@ 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(11)).toEqual("11th");
21+
expect(getOrdinalNumber(111)).toEqual("111th");
2022
});
23+
24+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
25+
expect(getOrdinalNumber(2)).toEqual("2nd");
26+
expect(getOrdinalNumber(22)).toEqual("22nd");
27+
expect(getOrdinalNumber(132)).toEqual("132nd");
28+
expect(getOrdinalNumber(12)).toEqual("12th");
29+
expect(getOrdinalNumber(112)).toEqual("112th");
30+
});
31+
32+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
33+
expect(getOrdinalNumber(3)).toEqual("3rd");
34+
expect(getOrdinalNumber(23)).toEqual("23rd");
35+
expect(getOrdinalNumber(133)).toEqual("133rd");
36+
expect(getOrdinalNumber(13)).toEqual("13th");
37+
expect(getOrdinalNumber(113)).toEqual("113th");
38+
});
39+
40+
test("should append 'th' for all other numbers", () => {
41+
expect(getOrdinalNumber(0)).toEqual("0th");
42+
expect(getOrdinalNumber(10)).toEqual("10th");
43+
expect(getOrdinalNumber(14)).toEqual("14th");
44+
expect(getOrdinalNumber(19)).toEqual("19th");
45+
expect(getOrdinalNumber(99)).toEqual("99th");
46+
expect(getOrdinalNumber(1000)).toEqual("1000th");
47+
});
48+
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count >= 0) {return str.repeat(count)}
3+
else throw new Error("Error")
34
}
45

56
module.exports = repeatStr;

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,32 @@ test("should repeat the string count times", () => {
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
2323

24+
test("should repeat the string count times", () => {
25+
const str = "hello";
26+
const count = 1;
27+
const repeatedStr = repeatStr(str, count);
28+
expect(repeatedStr).toEqual("hello");
29+
});
30+
2431
// Case: Handle count of 0:
2532
// Given a target string `str` and a `count` equal to 0,
2633
// When the repeatStr function is called with these inputs,
2734
// Then it should return an empty string.
2835

36+
test("should repeat the string count times", () => {
37+
const str = "hello";
38+
const count = 0;
39+
const repeatedStr = repeatStr(str, count);
40+
expect(repeatedStr).toEqual("");
41+
});
42+
2943
// Case: Handle negative count:
3044
// Given a target string `str` and a negative integer `count`,
3145
// When the repeatStr function is called with these inputs,
3246
// Then it should throw an error, as negative counts are not valid.
47+
48+
test("should repeat the string count times", () => {
49+
const str = "hello";
50+
const count = -1;
51+
expect(function(){repeatStr(str, count)}).toThrow()
52+
});

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"keywords": [],
1010
"author": "Code Your Future",
1111
"license": "ISC",
12-
"dependencies": {
13-
"jest": "^29.7.0"
12+
"devDependencies": {
13+
"jest": "^30.2.0"
1414
}
15-
}
15+
}

0 commit comments

Comments
 (0)