Skip to content

Commit 73a1500

Browse files
committed
tdd assignment
1 parent 3372770 commit 73a1500

File tree

6 files changed

+108
-6
lines changed

6 files changed

+108
-6
lines changed

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

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

511
module.exports = countChar;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,21 @@ 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 count zero occurrences of a character", () => {
26+
const str = "aaaaa";
27+
const char = "b";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
31+
32+
// Scenario: Non-consecutive occurrances
33+
// Given the input string `str`,
34+
// And a character `char` that appears in more than one block within `str`.
35+
// When the function is called with these inputs,
36+
// Then it should return the number of total occurrances.
37+
test("should count non-contiguous occurrences of a character", () => {
38+
const str = "aaaaabba";
39+
const char = "a";
40+
const count = countChar(str, char);
41+
expect(count).toEqual(6);
42+
});
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+
// 11 is a special case
3+
if (num === 11) {
4+
return "11th";
5+
}
6+
const lastDigit = num % 10;
7+
switch (lastDigit) {
8+
case 1:
9+
return String(num) + "st";
10+
case 2:
11+
return String(num) + "nd";
12+
case 3:
13+
return String(num) + "rd";
14+
default:
15+
return String(num) + "th";
16+
}
317
}
418

519
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
23+
// When the number ends with 2
24+
// Then the function should return a string by appending "nd" to the number.
25+
test("should append 'nd' for numbers ending with 2", () => {
26+
expect(getOrdinalNumber(2)).toEqual("2nd");
27+
expect(getOrdinalNumber(22)).toEqual("22nd");
28+
expect(getOrdinalNumber(132)).toEqual("132nd");
29+
});
30+
31+
// Case 3: Numbers ending with 3
32+
// When the number ends with 3
33+
// Then the function should return a string by appending "rd" to the number.
34+
test("should append 'rd' for numbers ending with 3", () => {
35+
expect(getOrdinalNumber(3)).toEqual("3rd");
36+
expect(getOrdinalNumber(23)).toEqual("23rd");
37+
expect(getOrdinalNumber(223)).toEqual("223rd");
38+
});
39+
40+
// Case 4: the general(ish) case
41+
// When numbers end with 0, 4, 5, 6, 7, 8, 9
42+
// The function should return a string by appending "th" to the number
43+
test("should append 'th' for numbers ending with 0, 4, 5, 6, 7, 8, 9", () => {
44+
expect(getOrdinalNumber(10)).toEqual("10th");
45+
expect(getOrdinalNumber(24)).toEqual("24th");
46+
expect(getOrdinalNumber(25)).toEqual("25th");
47+
expect(getOrdinalNumber(46)).toEqual("46th");
48+
expect(getOrdinalNumber(57)).toEqual("57th");
49+
expect(getOrdinalNumber(78)).toEqual("78th");
50+
expect(getOrdinalNumber(89)).toEqual("89th");
51+
});
52+
53+
// Case 5: The special case of 11
54+
// When the number is 11
55+
// Then the function should return a string by appending "th" to the number.
56+
test("should append 'th' for 11", () => {
57+
expect(getOrdinalNumber(11)).toEqual("11th");
58+
});
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("invalid input: negative number");
4+
}
5+
6+
if (count === 0) {
7+
return "";
8+
}
9+
10+
if ((count) => 1) {
11+
return str.repeat(count);
12+
}
313
}
414

515
module.exports = repeatStr;

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const repeatStr = require("./repeat-str");
88
// Given a target string `str` and a positive integer `count` greater than 1,
99
// When the repeatStr function is called with these inputs,
1010
// Then it should return a string that contains the original `str` repeated `count` times.
11-
12-
test("should repeat the string count times", () => {
11+
test("should repeat the string 3 times", () => {
1312
const str = "hello";
1413
const count = 3;
1514
const repeatedStr = repeatStr(str, count);
@@ -20,13 +19,30 @@ test("should repeat the string count times", () => {
2019
// Given a target string `str` and a `count` equal to 1,
2120
// When the repeatStr function is called with these inputs,
2221
// Then it should return the original `str` without repetition.
22+
test("should not repeat string with a count of 1", () => {
23+
const str = "hello";
24+
const count = 1;
25+
const repeatedStr = repeatStr(str, count);
26+
expect(repeatedStr).toEqual("hello");
27+
});
2328

2429
// Case: Handle count of 0:
2530
// Given a target string `str` and a `count` equal to 0,
2631
// When the repeatStr function is called with these inputs,
2732
// Then it should return an empty string.
33+
test("should return empty string with a count of 0", () => {
34+
const str = "hello";
35+
const count = 0;
36+
const repeatedStr = repeatStr(str, count);
37+
expect(repeatedStr).toEqual("");
38+
});
2839

2940
// Case: Handle negative count:
3041
// Given a target string `str` and a negative integer `count`,
3142
// When the repeatStr function is called with these inputs,
3243
// Then it should throw an error, as negative counts are not valid.
44+
test("should throw error when count < 0", () => {
45+
const str = "hello";
46+
const count = -2;
47+
expect(() => repeatStr(str, count)).toThrow();
48+
});

0 commit comments

Comments
 (0)