Skip to content

Commit 4e176f1

Browse files
Complete coursework/sprint-3-practice-tdd
1 parent e16389e commit 4e176f1

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
return [...stringOfCharacters].reduce(
3+
(acc, curr) => acc + (curr === findCharacter ? 1 : 0),
4+
0
5+
);
36
}
47

58
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, since there are no occurrences of a character", () => {
26+
const char = "a";
27+
const str = "AABBFFSAA";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const last2Digits = String(num).slice(-2);
3+
if (["11", "12", "13"].includes(last2Digits)) return `${num}th`;
4+
5+
const lastDigit = String(num).slice(-1);
6+
const restOfNum = String(num).slice(0, -1);
7+
let ordinalResult = "";
8+
switch (lastDigit) {
9+
case "1":
10+
ordinalResult = `${num}st`;
11+
break;
12+
case "2":
13+
ordinalResult = `${num}nd`;
14+
break;
15+
case "3":
16+
ordinalResult = `${num}rd`;
17+
break;
18+
default:
19+
ordinalResult = `${num}th`;
20+
}
21+
return ordinalResult;
322
}
423

524
module.exports = getOrdinalNumber;

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,41 @@ 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(31562)).toEqual("31562nd");
28+
expect(getOrdinalNumber(2322)).toEqual("2322nd");
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(3243)).toEqual("3243rd");
37+
expect(getOrdinalNumber(5462133)).toEqual("5462133rd");
38+
});
39+
40+
// Case 4: Numbers ending with 11, 12 or 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 with 11, 12 or 13", () => {
44+
expect(getOrdinalNumber(11)).toEqual("11th");
45+
expect(getOrdinalNumber(2111)).toEqual("2111th");
46+
expect(getOrdinalNumber(2113)).toEqual("2113th");
47+
expect(getOrdinalNumber(524312)).toEqual("524312th");
48+
});
49+
50+
// Case 4: Numbers not ending in 1, 2 and 3
51+
// When the number ends with 0, 4, 5, 6, 7, 8, 9
52+
// Then the function should return a string by appending "th" to the number.
53+
test("should append 'th' for numbers not ending in 1, 2 and 3", () => {
54+
expect(getOrdinalNumber(19)).toEqual("19th");
55+
expect(getOrdinalNumber(2118)).toEqual("2118th");
56+
expect(getOrdinalNumber(2110)).toEqual("2110th");
57+
expect(getOrdinalNumber(524334)).toEqual("524334th");
58+
});
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) throw new Error("Can not repeat negative times.");
3+
return str.repeat(count);
34
}
45

56
module.exports = repeatStr;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,30 @@ 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 count times", () => {
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 repeat the string count times", () => {
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 repeat the string count times", () => {
46+
const str = "hello";
47+
const count = -3;
48+
expect(() => repeatStr(str, count)).toThrow("Can not repeat negative times.");
49+
});

0 commit comments

Comments
 (0)