Skip to content

Commit 30a0239

Browse files
committed
sprint 3 TDD solution
1 parent 3372770 commit 30a0239

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
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++;
6+
} else {
7+
return 0;
8+
}
9+
}
10+
return count;
311
}
412

513
module.exports = countChar;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ 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' if character does not exist", () => {
26+
expect(countChar("edak", "s")).toEqual(0);
27+
});
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+
const numStr = num.toString();
3+
const numLength = numStr.length;
4+
if (numStr[numLength - 2] === "1" && numStr[numLength - 1] === "1") {
5+
return num + "th";
6+
} else if (numStr[numLength - 1] === "1") {
7+
return num + "st";
8+
} else if (numStr[numLength - 2] === "1" && numStr[numLength - 1] === "3") {
9+
return num + "th";
10+
} else if (numStr[numLength - 1] === "3") {
11+
return num + "rd";
12+
} else if (numStr[numLength - 1] === "2") {
13+
return num + "nd";
14+
} else {
15+
return num + "th";
16+
}
317
}
418

519
module.exports = getOrdinalNumber;

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,43 @@ 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 11
23+
test("should return 'th' for numbers ending with 11", () => {
24+
expect(getOrdinalNumber(11)).toEqual("11th");
25+
expect(getOrdinalNumber(111)).toEqual("111th");
26+
expect(getOrdinalNumber(211)).toEqual("211th");
27+
expect(getOrdinalNumber(1011)).toEqual("1011th");
28+
});
29+
30+
//case:3 Numbers ending with 3 but not 13
31+
test("Should return 'rd' for numbers ending with 3", () => {
32+
expect(getOrdinalNumber(73)).toEqual("73rd");
33+
expect(getOrdinalNumber(93)).toEqual("93rd");
34+
expect(getOrdinalNumber(43)).toEqual("43rd");
35+
expect(getOrdinalNumber(2003)).toEqual("2003rd");
36+
});
37+
38+
//case:4 expect Numbers ending with 13 to return th
39+
test(" should return 'th' for numbers ending with 13", () => {
40+
expect(getOrdinalNumber(13)).toEqual("13th");
41+
expect(getOrdinalNumber(413)).toEqual("413th");
42+
expect(getOrdinalNumber(613)).toEqual("613th");
43+
});
44+
//case 4: expect number ending with 2 to return nd
45+
test(" should return 'nd' for numbers ending with 2", () => {
46+
expect(getOrdinalNumber(22)).toEqual("22nd");
47+
expect(getOrdinalNumber(42)).toEqual("42nd");
48+
expect(getOrdinalNumber(102)).toEqual("102nd");
49+
});
50+
51+
// case 5: expect number not included in the above case to return th
52+
test("should return 'th' for numbers ending with 4, 5,6,7,8,9,0", () => {
53+
expect(getOrdinalNumber(4)).toEqual("4th");
54+
expect(getOrdinalNumber(5)).toEqual("5th");
55+
expect(getOrdinalNumber(6)).toEqual("6th");
56+
expect(getOrdinalNumber(7)).toEqual("7th");
57+
expect(getOrdinalNumber(8)).toEqual("8th");
58+
expect(getOrdinalNumber(9)).toEqual("9th");
59+
expect(getOrdinalNumber(10)).toEqual("10th");
60+
});
Lines changed: 8 additions & 2 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 >= 1) {
3+
return str.repeat(count);
4+
} else if (count === 0) {
5+
return "";
6+
} else {
7+
throw new Error("Invalid count");
8+
}
39
}
410

511
module.exports = repeatStr;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,23 @@ 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 without repetition ", () => {
24+
expect(repeatStr("edak", 1)).toEqual("edak");
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 return an 'empty string' for count value of 0", () => {
32+
expect(repeatStr("edak", 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+
40+
test("should throw and error if count value is negative integer", () => {
41+
expect(() => repeatStr("edak", -1)).toThrow("Invalid count");
42+
});

0 commit comments

Comments
 (0)