Skip to content

Commit 3d64708

Browse files
committed
updated files
1 parent 29ef74a commit 3d64708

6 files changed

Lines changed: 85 additions & 4 deletions

File tree

‎practice-tdd/count.js‎

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

‎practice-tdd/count.test.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// implement a function countChar that counts the number of times a character occurs in a string
22
import { countChar } from "./count.js";
3+
34
// Given a string `str` and a single character `char` to search for,
45
// When the countChar function is called with these inputs,
56
// Then it should:
@@ -22,3 +23,17 @@ test("should count multiple occurrences of a character", () => {
2223
// And a character `char` that does not exist within `str`.
2324
// When the function is called with these inputs,
2425
// Then it should return 0, indicating that no occurrences of `char` were found.
26+
27+
test("should count multiple occurrences of a character", () => {
28+
const str = "aaaaa";
29+
const char = "z";
30+
const count = countChar(str, char);
31+
expect(count).toEqual(0);
32+
});
33+
34+
test("should count multiple occurrences of a character", () => {
35+
const str = "aaaaa";
36+
const char = "7";
37+
const count = countChar(str, char);
38+
expect(count).toEqual(0);
39+
});

‎practice-tdd/get-ordinal-number.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
export function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastTwoDigits = num % 100;
3+
4+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
5+
return `${num}th`;
6+
}
7+
8+
const lastDigit = num % 10;
9+
if (lastDigit === 1) return `${num}st`;
10+
if (lastDigit === 2) return `${num}nd`;
11+
if (lastDigit === 3) return `${num}rd`;
12+
13+
return `${num}th`;
314
}

‎practice-tdd/get-ordinal-number.test.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,21 @@ 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(122)).toEqual("122nd");
26+
});
27+
28+
test("should append 'rd' for numbers ending with 3", () => {
29+
expect(getOrdinalNumber(3)).toEqual("3rd");
30+
expect(getOrdinalNumber(23)).toEqual("23rd");
31+
expect(getOrdinalNumber(123)).toEqual("123rd");
32+
});
33+
34+
test("should append 'th' for numbers ending with 4, except those ending with 0", () => {
35+
expect(getOrdinalNumber(4)).toEqual("4th");
36+
expect(getOrdinalNumber(144)).toEqual("144th");
37+
expect(getOrdinalNumber(40)).toEqual("40th");
38+
});

‎practice-tdd/repeat-str.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
export function repeatStr() {
1+
export function repeatStr(str, count) {
22
// 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).
33
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
4+
5+
if (count < 0) {
6+
throw new Error("negative counts are not valid");
7+
}
8+
let result = "";
9+
10+
for (let i = 0; i < count; i++) {
11+
result += str;
12+
}
13+
return result;
514
}

‎practice-tdd/repeat-str.test.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,32 @@ 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 throw an error, as negative counts are not valid", () => {
49+
const str = "hello";
50+
const count = -1;
51+
expect(() => repeatStr(str, count)).toThrow("negative counts are not valid");
52+
});

0 commit comments

Comments
 (0)