Skip to content

Commit f4e9417

Browse files
Coursework 2-practice-tdd
1 parent 24151e5 commit f4e9417

File tree

6 files changed

+107
-31
lines changed

6 files changed

+107
-31
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
return stringOfCharacters
3+
.split("")
4+
.reduce((acc, curr) => acc + (curr === findCharacter ? 1 : 0), 0);
35
}
46

57
module.exports = countChar;
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// implement a function countChar that counts the number of times a character occurs in a string
22
const countChar = require("./count");
3-
// Given a string str and a single character char to search for,
3+
// Given a string `str` and a single character `char` to search for,
44
// When the countChar function is called with these inputs,
55
// Then it should:
66

77
// Scenario: Multiple Occurrences
8-
// Given the input string str,
9-
// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'),
8+
// Given the input string `str`,
9+
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
1010
// When the function is called with these inputs,
11-
// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa').
11+
// Then it should correctly count occurrences of `char`.
1212

1313
test("should count multiple occurrences of a character", () => {
1414
const str = "aaaaa";
@@ -18,7 +18,14 @@ test("should count multiple occurrences of a character", () => {
1818
});
1919

2020
// Scenario: No Occurrences
21-
// Given the input string str,
22-
// And a character char that does not exist within the case-sensitive str,
21+
// Given the input string `str`,
22+
// And a character `char` that does not exist within `str`.
2323
// When the function is called with these inputs,
24-
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
24+
25+
// Then it should return 0, indicating that no occurrences of `char` were found.
26+
test("should return 0, since there are no occurrences of a character", () => {
27+
const char = "a";
28+
const str = "AABBFFSAA";
29+
const count = countChar(str, char);
30+
expect(count).toEqual(0);
31+
});
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
if ([11, 12, 13].includes(num)) return `${num}th`;
3+
const lastDigit = String(num).slice(-1);
4+
const restOfNum = String(num).slice(0, -1);
5+
let ordinalResult = "";
6+
switch (lastDigit) {
7+
case "1":
8+
ordinalResult = restOfNum + "1st";
9+
break;
10+
case "2":
11+
ordinalResult = restOfNum + "2nd";
12+
break;
13+
case "3":
14+
ordinalResult = restOfNum + "3rd";
15+
break;
16+
default:
17+
ordinalResult = restOfNum + lastDigit + "th";
18+
}
19+
return ordinalResult;
320
}
421

522
module.exports = getOrdinalNumber;
Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,46 @@
11
const getOrdinalNumber = require("./get-ordinal-number");
2-
// In this week's prep, we started implementing getOrdinalNumber
2+
// In this week's prep, we started implementing getOrdinalNumber.
33

4-
// continue testing and implementing getOrdinalNumber for additional cases
5-
// Write your tests using Jest - remember to run your tests often for continual feedback
4+
// Continue testing and implementing getOrdinalNumber for additional cases.
5+
// Write your tests using Jest remember to run your tests often for continual feedback.
66

7-
// Case 1: Identify the ordinal number for 1
8-
// When the number is 1,
9-
// Then the function should return "1st"
7+
// To ensure thorough testing, we need broad scenarios that cover all possible cases.
8+
// Listing individual values, however, can quickly lead to an unmanageable number of test cases.
9+
// Instead of writing tests for individual numbers, consider grouping all possible input values
10+
// into meaningful categories. Then, select representative samples from each category to test.
11+
// This approach improves coverage and makes our tests easier to maintain.
1012

11-
test("should return '1st' for 1", () => {
13+
// Case 1: Numbers ending with 1 (but not 11)
14+
// When the number ends with 1, except those ending with 11,
15+
// 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", () => {
1217
expect(getOrdinalNumber(1)).toEqual("1st");
18+
expect(getOrdinalNumber(21)).toEqual("21st");
19+
expect(getOrdinalNumber(31)).toEqual("131st");
20+
});
21+
22+
test("should return '2nd' for 2", () => {
23+
expect(getOrdinalNumber(2)).toEqual("2nd");
24+
});
25+
26+
test("should return '3rd' for 3", () => {
27+
expect(getOrdinalNumber(3)).toEqual("3rd");
28+
});
29+
test("should return '4th' for 4", () => {
30+
expect(getOrdinalNumber(4)).toEqual("4th");
31+
});
32+
test("should return '5th' for 5", () => {
33+
expect(getOrdinalNumber(5)).toEqual("5th");
34+
});
35+
test("should return '11th' for 11", () => {
36+
expect(getOrdinalNumber(11)).toEqual("11th");
37+
});
38+
test("should return '21st' for 21", () => {
39+
expect(getOrdinalNumber(21)).toEqual("21st");
40+
});
41+
test("should return '32nd' for 32", () => {
42+
expect(getOrdinalNumber(32)).toEqual("32nd");
43+
});
44+
test("should return '53rd' for 53", () => {
45+
expect(getOrdinalNumber(53)).toEqual("53rd");
1346
});
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;
Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Implement a function repeatStr
22
const repeatStr = require("./repeat-str");
3-
// Given a target string str and a positive integer count,
3+
// Given a target string `str` and a positive integer `count`,
44
// When the repeatStr function is called with these inputs,
55
// Then it should:
66

7-
// case: repeat String:
8-
// Given a target string str and a positive integer count,
7+
// Case: handle multiple repetitions:
8+
// Given a target string `str` and a positive integer `count` greater than 1,
99
// When the repeatStr function is called with these inputs,
10-
// Then it should repeat the str count times and return a new string containing the repeated str values.
10+
// Then it should return a string that contains the original `str` repeated `count` times.
1111

1212
test("should repeat the string count times", () => {
1313
const str = "hello";
@@ -16,17 +16,33 @@ test("should repeat the string count times", () => {
1616
expect(repeatedStr).toEqual("hellohellohello");
1717
});
1818

19-
// case: handle Count of 1:
20-
// Given a target string str and a count equal to 1,
19+
// Case: handle count of 1:
20+
// Given a target string `str` and a `count` equal to 1,
2121
// When the repeatStr function is called with these inputs,
22-
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
22+
test("should repeat the string count times", () => {
23+
const str = "hello";
24+
const count = 1;
25+
const repeatedStr = repeatStr(str, count);
26+
expect(repeatedStr).toEqual("hello");
27+
});
2328

24-
// case: Handle Count of 0:
25-
// Given a target string str and a count equal to 0,
29+
// Case: Handle count of 0:
30+
// Given a target string `str` and a `count` equal to 0,
2631
// When the repeatStr function is called with these inputs,
27-
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
32+
// Then it should return an empty string.
33+
test("should repeat the string count times", () => {
34+
const str = "hello";
35+
const count = 0;
36+
const repeatedStr = repeatStr(str, count);
37+
expect(repeatedStr).toEqual("");
38+
});
2839

29-
// case: Negative Count:
30-
// Given a target string str and a negative integer count,
40+
// Case: Handle negative count:
41+
// Given a target string `str` and a negative integer `count`,
3142
// When the repeatStr function is called with these inputs,
32-
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
43+
// Then it should throw an error, as negative counts are not valid.
44+
test("should repeat the string count times", () => {
45+
const str = "hello";
46+
const count = -3;
47+
expect(() => repeatStr(str, count)).toThrow("Can not repeat negative times.");
48+
});

0 commit comments

Comments
 (0)