Skip to content

Commit 81fc9ed

Browse files
committed
complete TDD
1 parent 168ce77 commit 81fc9ed

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
const countChar = require("./count");
33
// Given a string `str` and a single character `char` to search for,
44
// When the countChar function is called with these inputs,
5-
// Then it should:
5+
// Then it should:
6+
test("should count occurrences of a character", () => {
7+
expect(countChar("tired", "d")).toEqual(1)
8+
})
9+
610

711
// Scenario: Multiple Occurrences
812
// Given the input string `str`,
@@ -22,3 +26,6 @@ test("should count multiple occurrences of a character", () => {
2226
// And a character `char` that does not exist within `str`.
2327
// When the function is called with these inputs,
2428
// Then it should return 0, indicating that no occurrences of `char` were found.
29+
test("should count no occurrences of a character", () => {
30+
expect(countChar("bananataste","w")).toEqual(0)
31+
})
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
lastDigit = num % 10;
3+
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

21+
console.log(getOrdinalNumber(112))
22+
23+
24+
525
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
@@ -18,3 +18,31 @@ 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: last two digits ending with 11,12,13
23+
test("should append 'th' for numbers with last digits 11,12,13", () =>{
24+
expect(getOrdinalNumber(11)).toEqual("11th");
25+
expect(getOrdinalNumber(313)).toEqual("313th")
26+
expect(getOrdinalNumber(2112)).toEqual("2112th")
27+
})
28+
29+
// Case 3: Numbers ending with 2(but not 12)
30+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
31+
expect(getOrdinalNumber(2)).toEqual("2nd");
32+
expect(getOrdinalNumber(52)).toEqual("52nd");
33+
expect(getOrdinalNumber(232)).toEqual("232nd");
34+
});
35+
36+
// Case 4: Numbers ending with 3(but not 13)
37+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
38+
expect(getOrdinalNumber(3)).toEqual("3rd");
39+
expect(getOrdinalNumber(83)).toEqual("83rd");
40+
expect(getOrdinalNumber(463)).toEqual("463rd");
41+
});
42+
43+
// case 5: Numbers ending with 4-9 and 0
44+
test("should append 'th' for numbers with last 4-9 and 0", () =>{
45+
expect(getOrdinalNumber(8)).toEqual("8th");
46+
expect(getOrdinalNumber(3130)).toEqual("3130th")
47+
expect(getOrdinalNumber(2119)).toEqual("2119th")
48+
})
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str,count) {
2+
if (count>0) return str.repeat(count)
3+
if (count===0) return ""
4+
if (count<0) return"invalid count"
35
}
46

5-
module.exports = repeatStr;
7+
console.log(repeatStr("hello",5))
8+
console.log(repeatStr("nihao",1))
9+
console.log(repeatStr("nihao",-5))
10+
11+
module.exports = repeatStr;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@ 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 repeat the string only once", () => {
24+
expect(repeatStr("nihao",1)).toEqual("nihao");
25+
});
2326

2427
// Case: Handle count of 0:
2528
// Given a target string `str` and a `count` equal to 0,
2629
// When the repeatStr function is called with these inputs,
2730
// Then it should return an empty string.
31+
test("should repeat the string 0", () => {
32+
expect(repeatStr("newyear",0)).toEqual("");
33+
});
2834

2935
// Case: Handle negative count:
3036
// Given a target string `str` and a negative integer `count`,
3137
// When the repeatStr function is called with these inputs,
3238
// Then it should throw an error, as negative counts are not valid.
39+
test("should repeat the string negative times", () => {
40+
expect(repeatStr("celebration", -5)).toEqual("invalid count");
41+
});

0 commit comments

Comments
 (0)