Skip to content

Commit 8977e58

Browse files
committed
Complete 2-practice-tdd
1 parent ecf8ae1 commit 8977e58

File tree

6 files changed

+91
-5
lines changed

6 files changed

+91
-5
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
// trying to find the amount of times the findCharacter is found in the stringOfCharacters
3+
const lengthOfString = stringOfCharacters.length;
4+
console.log("lengthOfString", lengthOfString);
5+
6+
// replace all instances of findCharacter in string of characters with an empty string, and then get the length. (e.g. in house replace the e with an empty string " ").
7+
const otherLettersLength = stringOfCharacters.replaceAll(
8+
findCharacter,
9+
""
10+
).length;
11+
12+
// declaring the variable called result and subtract otherLettersLength from the lengthOfString to find the number of occurences of findCharacter.
13+
const result = lengthOfString - otherLettersLength;
14+
console.log("result", result);
15+
return result;
316
}
417

518
module.exports = countChar;
19+
20+
// function countOccurrences(str, char) {
21+
// return str.length - str.replaceAll(char, "").length;
22+
// }

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,30 @@ test("should count multiple occurrences of a character", () => {
1717
expect(count).toEqual(5);
1818
});
1919

20+
test("should count multiple occurrences of a character", () => {
21+
const str = "house";
22+
const char = "e";
23+
const count = countChar(str, char);
24+
expect(count).toEqual(1);
25+
});
26+
27+
test("should count multiple occurrences of a character", () => {
28+
const str = "playful";
29+
const char = "l";
30+
const count = countChar(str, char);
31+
expect(count).toEqual(2);
32+
});
33+
2034
// Scenario: No Occurrences
2135
// Given the input string `str`,
2236
// And a character `char` that does not exist within `str`.
2337
// When the function is called with these inputs,
2438
// Then it should return 0, indicating that no occurrences of `char` were found.
2539

2640
// test(``);
41+
test("should return when there are no occurrences of a character", () => {
42+
const str = "spaceship";
43+
const char = "x";
44+
const count = countChar(str, char);
45+
expect(count).toEqual(0);
46+
});
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastdigit = num % 10;
3+
if (lastdigit === 1) {
4+
return `${num}st`;
5+
}
6+
7+
if (lastdigit === 2) {
8+
return `${num}nd`;
9+
}
10+
11+
if (lastdigit === 3) {
12+
return `${num}rd`;
13+
} else {
14+
return `${num}th`;
15+
}
316
}
417

518
module.exports = getOrdinalNumber;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,15 @@ 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+
test("should append 'nd' for numbers ending with 2", () => {
23+
expect(getOrdinalNumber(2)).toEqual("2nd");
24+
expect(getOrdinalNumber(22)).toEqual("22nd");
25+
expect(getOrdinalNumber(132)).toEqual("132nd");
26+
});
27+
28+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
29+
expect(getOrdinalNumber(3)).toEqual("3rd");
30+
expect(getOrdinalNumber(33)).toEqual("33rd");
31+
expect(getOrdinalNumber(133)).toEqual("133rd");
32+
});
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("invalid count");
4+
}
5+
return str.repeat(count);
36
}
4-
7+
// call the repeat function on the string and pass in count.
58
module.exports = repeatStr;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,33 @@ test("should repeat the string count times", () => {
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
2323

24+
test("should repeat the string count times", () => {
25+
const str = "hello";
26+
const count = 1;
27+
const repeatedStr = repeatStr(str, count);
28+
expect(repeatedStr).toEqual("hello");
29+
});
30+
2431
// Case: Handle count of 0:
2532
// Given a target string `str` and a `count` equal to 0,
2633
// When the repeatStr function is called with these inputs,
2734
// Then it should return an empty string.
2835

36+
test("should repeat the string count times", () => {
37+
const str = "hello";
38+
const count = 0;
39+
const repeatedStr = repeatStr(str, count);
40+
expect(repeatedStr).toEqual("");
41+
});
42+
2943
// Case: Handle negative count:
3044
// Given a target string `str` and a negative integer `count`,
3145
// When the repeatStr function is called with these inputs,
3246
// Then it should throw an error, as negative counts are not valid.
47+
48+
test("should repeat the string count times", () => {
49+
const str = "hello";
50+
const count = -2;
51+
// const repeatedStr = repeatStr(str, count);
52+
expect(() => repeatStr(str, count)).toThrow("invalid count");
53+
});

0 commit comments

Comments
 (0)