Skip to content

Commit 3289b60

Browse files
committed
task 2 of spring 3 clean
1 parent 8f3d6cf commit 3289b60

File tree

6 files changed

+73
-7
lines changed

6 files changed

+73
-7
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 count = 0;
3+
for (let i = 0; i < stringOfCharacters.length; i++) {
4+
if (stringOfCharacters[i] === findCharacter) {
5+
count++;
6+
}
7+
}
8+
return count;
39
}
410

511
module.exports = countChar;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character char that does not exist within the case-sensitive str,
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
25+
26+
test("should return 0 when the character does not exist in the string", () => {
27+
const str = "hello";
28+
const char = "z";
29+
const count = countChar(str, char);
30+
expect(count).toEqual(0);
31+
});
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
function getOrdinalNumber(num) {
2-
return "1st";
1+
function getOrdinalNumber(number) {
2+
if (number === 1) return "1st";
3+
if (number === 2) return "2nd";
4+
if (number === 3) return "3rd";
5+
return number + "th";
36
}
47

58
module.exports = getOrdinalNumber;

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// get-ordinal-number.test.js
12
const getOrdinalNumber = require("./get-ordinal-number");
23
// In this week's prep, we started implementing getOrdinalNumber
34

@@ -7,7 +8,27 @@ const getOrdinalNumber = require("./get-ordinal-number");
78
// Case 1: Identify the ordinal number for 1
89
// When the number is 1,
910
// Then the function should return "1st"
10-
1111
test("should return '1st' for 1", () => {
1212
expect(getOrdinalNumber(1)).toEqual("1st");
1313
});
14+
15+
// Case 2: Identify the ordinal number for 2
16+
// When the number is 2,
17+
// Then the function should return "2nd"
18+
test("should return '2nd' for 2", () => {
19+
expect(getOrdinalNumber(2)).toEqual("2nd");
20+
});
21+
22+
// Case 3: Identify the ordinal number for 3
23+
// When the number is 3,
24+
// Then the function should return "3rd"
25+
test("should return '3rd' for 3", () => {
26+
expect(getOrdinalNumber(3)).toEqual("3rd");
27+
});
28+
29+
// Case 4: Identify the ordinal number for 4
30+
// When the number is 4,
31+
// Then the function should return "4th"
32+
test("should return '4th' for 4", () => {
33+
expect(getOrdinalNumber(4)).toEqual("4th");
34+
});

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
function repeat() {
2-
return "hellohellohello";
1+
function repeat(str, count) {
2+
if (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+
return result;
311
}
412

513
module.exports = repeat;

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Implement a function repeat
21
const repeat = require("./repeat");
32
// Given a target string str and a positive integer count,
43
// When the repeat function is called with these inputs,
@@ -21,12 +20,34 @@ test("should repeat the string count times", () => {
2120
// When the repeat function is called with these inputs,
2221
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
2322

23+
test("should return the original string when count is 1", () => {
24+
const str = "hi";
25+
const count = 1;
26+
const repeatedStr = repeat(str, count);
27+
expect(repeatedStr).toEqual("hi");
28+
});
29+
2430
// case: Handle Count of 0:
2531
// Given a target string str and a count equal to 0,
2632
// When the repeat function is called with these inputs,
2733
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
2834

35+
test("should return an empty string when count is 0", () => {
36+
const str = "hi";
37+
const count = 0;
38+
const repeatedStr = repeat(str, count);
39+
expect(repeatedStr).toEqual("");
40+
});
41+
2942
// case: Negative Count:
3043
// Given a target string str and a negative integer count,
3144
// When the repeat function is called with these inputs,
3245
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
46+
47+
test("should throw an error when count is negative", () => {
48+
const str = "hi";
49+
const count = -2;
50+
expect(() => repeat(str, count)).toThrow(
51+
"Count must be a non-negative integer"
52+
);
53+
});

0 commit comments

Comments
 (0)