Skip to content

Commit c922c69

Browse files
committed
Implement countChar, repeatStr, and getOrdinalNumber functions with TDD
1 parent b31a586 commit c922c69

6 files changed

Lines changed: 77 additions & 5 deletions

File tree

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

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

513
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 `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+
26+
test("should count no occurrences of a character", () => {
27+
const str = "aaaaa";
28+
const char = "b";
29+
const count = countChar(str, char);
30+
expect(count).toEqual(0);
31+
});
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
if (num % 10 === 1) {
3+
return `${num}st`;
4+
}
5+
6+
if (num % 10 === 2) {
7+
return `${num}nd`;
8+
}
9+
10+
if (num % 10 === 3) {
11+
return `${num}rd`;
12+
}
13+
14+
if (num % 100 === 11 || num % 100 === 12 || num % 100 === 13) {
15+
return `${num}th`;
16+
}
17+
18+
return `${num}th`;
319
}
420

521
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, except those ending with 12", () => {
23+
expect(getOrdinalNumber(2)).toEqual("2nd");
24+
expect(getOrdinalNumber(22)).toEqual("22nd");
25+
expect(getOrdinalNumber(102)).toEqual("102nd");
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(23)).toEqual("23rd");
31+
expect(getOrdinalNumber(103)).toEqual("103rd");
32+
});
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
function repeatStr() {
2-
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
3-
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
2+
if (count < 0) {
3+
throw new Error("invalid");
4+
}
5+
6+
let result = "";
7+
8+
for (let i = 0; i < count; i++) {
9+
result += str;
10+
}
11+
12+
return result;
513
}
614

715
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 1 time", () => {
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 not repeat the string", () => {
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 throw an error for negative count", () => {
49+
const str = "hello";
50+
const count = -1;
51+
const repeatedStr = repeatStr(str, count);
52+
expect(() => repeatStr(str, count)).toThrow();
53+
});

0 commit comments

Comments
 (0)