Skip to content

Commit 07b6a4f

Browse files
committed
Sprint 3 practice tdd
1 parent 8f3d6cf commit 07b6a4f

File tree

6 files changed

+93
-8
lines changed

6 files changed

+93
-8
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
12
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
3+
let total = 0;
4+
for (let i=0; i < stringOfCharacters.length; i++){
5+
if (findCharacter == stringOfCharacters[i]){
6+
total++;
7+
}
8+
}
9+
return total;
310
}
411

512
module.exports = countChar;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@ test("should count multiple occurrences of a character", () => {
1818
});
1919

2020
// Scenario: No Occurrences
21+
test("No Occurrences", () => {
22+
const str = "bcdefg";
23+
const char = "a";
24+
const count = countChar(str, char);
25+
expect(count).toEqual(0);
26+
});
2127
// Given the input string str,
2228
// And a character char that does not exist within the case-sensitive str,
2329
// When the function is called with these inputs,
2430
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
31+
test("Case-sensitive str", () => {
32+
const str = "Abcdefg";
33+
const char = "a";
34+
const count = countChar(str, char);
35+
expect(count).toEqual(0);
36+
});
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
3-
}
4-
2+
let result;
3+
if (num % 100 == 11 || num % 100 == 12 || num % 100 == 13){
4+
result = num.toString() + "th";
5+
}
6+
else if (num % 10 == 1){
7+
result = num.toString() +"st";
8+
}
9+
else if (num % 10 == 2){
10+
result = num.toString() + "nd";
11+
}
12+
else if (num % 10 == 3){
13+
result = num.toString() + "rd";
14+
}
15+
else {
16+
result = num.toString() + "th";
17+
}
18+
19+
return result
20+
}
521
module.exports = getOrdinalNumber;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,26 @@ const getOrdinalNumber = require("./get-ordinal-number");
1111
test("should return '1st' for 1", () => {
1212
expect(getOrdinalNumber(1)).toEqual("1st");
1313
});
14+
15+
test("should return '2nd' for 2", () => {
16+
expect(getOrdinalNumber(2)).toEqual("2nd");
17+
});
18+
19+
test("should return '3rd' for 3", () => {
20+
expect(getOrdinalNumber(3)).toEqual("3rd");
21+
});
22+
23+
test("should return '4th' for 4", () => {
24+
expect(getOrdinalNumber(4)).toEqual("4th");
25+
});
26+
test("should return '11th' for 11", () => {
27+
expect(getOrdinalNumber(11)).toEqual("11th");
28+
});
29+
30+
test("should return '12th' for 12", () => {
31+
expect(getOrdinalNumber(12)).toEqual("12th");
32+
});
33+
34+
test("should return '13th' for 13", () => {
35+
expect(getOrdinalNumber(13)).toEqual("13th");
36+
});

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
function repeat() {
2-
return "hellohellohello";
1+
function repeat(str, count) {
2+
3+
if (count < 0){
4+
return null
5+
}
6+
else if (count == 0){
7+
return ""
8+
}
9+
let result = "";
10+
for ( let i=0; i<count; i++){
11+
result +=str
12+
}
13+
return result
314
}
415

516
module.exports = repeat;

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,29 @@ test("should repeat the string count times", () => {
2020
// Given a target string str and a count equal to 1,
2121
// When the repeat function is called with these inputs,
2222
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
23-
23+
test("handle Count of 1", () => {
24+
const str = "hello";
25+
const count = 1;
26+
const repeatedStr = repeat(str, count);
27+
expect(repeatedStr).toEqual("hello");
28+
});
2429
// case: Handle Count of 0:
2530
// Given a target string str and a count equal to 0,
2631
// When the repeat function is called with these inputs,
2732
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
28-
33+
test("handle Count of 0", () => {
34+
const str = "hello";
35+
const count = 0;
36+
const repeatedStr = repeat(str, count);
37+
expect(repeatedStr).toEqual("");
38+
});
2939
// case: Negative Count:
3040
// Given a target string str and a negative integer count,
3141
// When the repeat function is called with these inputs,
3242
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
43+
test("Negative Count", () => {
44+
const str = "hello";
45+
const count = -1;
46+
const repeatedStr = repeat(str, count);
47+
expect(repeatedStr).toEqual(null);
48+
});

0 commit comments

Comments
 (0)