Skip to content

Commit 23b8ce8

Browse files
committed
Complete Sprint 3 TDD practice tasks including count, ordinal numbers, and repeat string
1 parent 3372770 commit 23b8ce8

File tree

6 files changed

+93
-10
lines changed

6 files changed

+93
-10
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+
4+
for (let i = 0; i < stringOfCharacters.length; i++) {
5+
if (stringOfCharacters[i] === findCharacter) {
6+
count++;
7+
}
8+
}
9+
10+
return count;
311
}
412

513
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 when character does not exist in the string", () => {
26+
const str = "hello world";
27+
const char = "x";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastDigit = num % 10;
3+
const lastTwoDigits = num % 100;
4+
5+
if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) {
6+
return num + "th";
7+
}
8+
9+
if (lastDigit === 1) {
10+
return num + "st";
11+
} else if (lastDigit === 2) {
12+
return num + "nd";
13+
} else if (lastDigit === 3) {
14+
return num + "rd";
15+
} else {
16+
return num + "th";
17+
}
318
}
419

520
module.exports = getOrdinalNumber;

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,36 @@ const getOrdinalNumber = require("./get-ordinal-number");
1313
// Case 1: Numbers ending with 1 (but not 11)
1414
// When the number ends with 1, except those ending with 11,
1515
// Then the function should return a string by appending "st" to the number.
16-
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
16+
test("should append 'st' for numbers ending with 1, except 11", () => {
1717
expect(getOrdinalNumber(1)).toEqual("1st");
1818
expect(getOrdinalNumber(21)).toEqual("21st");
19-
expect(getOrdinalNumber(131)).toEqual("131st");
19+
expect(getOrdinalNumber(31)).toEqual("31st");
20+
expect(getOrdinalNumber(101)).toEqual("101st");
21+
});
22+
23+
test("should append 'nd' for numbers ending with 2, except 12", () => {
24+
expect(getOrdinalNumber(2)).toEqual("2nd");
25+
expect(getOrdinalNumber(22)).toEqual("22nd");
26+
expect(getOrdinalNumber(82)).toEqual("82nd");
27+
});
28+
29+
test("should append 'rd' for numbers ending with 3, except 13", () => {
30+
expect(getOrdinalNumber(3)).toEqual("3rd");
31+
expect(getOrdinalNumber(33)).toEqual("33rd");
32+
expect(getOrdinalNumber(93)).toEqual("93rd");
33+
});
34+
35+
test("should append 'th' for the exceptions 11, 12, and 13", () => {
36+
expect(getOrdinalNumber(11)).toEqual("11th");
37+
expect(getOrdinalNumber(12)).toEqual("12th");
38+
expect(getOrdinalNumber(13)).toEqual("13th");
39+
expect(getOrdinalNumber(111)).toEqual("111th");
40+
});
41+
42+
test("should append 'th' for all other numbers", () => {
43+
expect(getOrdinalNumber(4)).toEqual("4th");
44+
expect(getOrdinalNumber(10)).toEqual("10th");
45+
expect(getOrdinalNumber(35)).toEqual("35th");
46+
expect(getOrdinalNumber(99)).toEqual("99th");
47+
expect(getOrdinalNumber(100)).toEqual("100th");
2048
});
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (typeof count !== "number" || count < 0) {
3+
throw new Error("Count must be a non-negative integer");
4+
}
5+
6+
let result = "";
7+
for (let i = 0; i < count; i++) {
8+
result += str;
9+
}
10+
11+
return result;
312
}
413

514
module.exports = repeatStr;

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,40 @@ const repeatStr = require("./repeat-str");
1010
// Then it should return a string that contains the original `str` repeated `count` times.
1111

1212
test("should repeat the string count times", () => {
13-
const str = "hello";
14-
const count = 3;
15-
const repeatedStr = repeatStr(str, count);
16-
expect(repeatedStr).toEqual("hellohellohello");
13+
expect(repeatStr("hello", 3)).toEqual("hellohellohello");
1714
});
1815

1916
// Case: handle count of 1:
2017
// Given a target string `str` and a `count` equal to 1,
2118
// When the repeatStr function is called with these inputs,
2219
// Then it should return the original `str` without repetition.
2320

21+
test("should return the original string when count is 1", () => {
22+
expect(repeatStr("world", 1)).toEqual("world");
23+
});
24+
2425
// Case: Handle count of 0:
2526
// Given a target string `str` and a `count` equal to 0,
2627
// When the repeatStr function is called with these inputs,
2728
// Then it should return an empty string.
2829

30+
test("should return an empty string when count is 0", () => {
31+
expect(repeatStr("test", 0)).toEqual("");
32+
});
33+
2934
// Case: Handle negative count:
3035
// Given a target string `str` and a negative integer `count`,
3136
// When the repeatStr function is called with these inputs,
3237
// Then it should throw an error, as negative counts are not valid.
38+
39+
test("should throw an error when count is negative", () => {
40+
expect(() => {
41+
repeatStr("error", -2);
42+
}).toThrow("Count must be a non-negative integer");
43+
});
44+
45+
test("should throw an error when count is not a number", () => {
46+
expect(() => {
47+
repeatStr("error", "-2");
48+
}).toThrow("Count must be a non-negative integer");
49+
});

0 commit comments

Comments
 (0)