Skip to content

Commit c382bec

Browse files
committed
sprint 3 practice-tdd complete
1 parent 3372770 commit c382bec

File tree

6 files changed

+95
-11
lines changed

6 files changed

+95
-11
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,20 @@ 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+
test("it should return 0 because the character doesn't occurrences", () => {
26+
const str = "The character have no match";
27+
const char = "z";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
31+
// Scenario: 1 Occurrences
32+
// Given the input string `str`,
33+
// And a character `char` that have 1 character exist in `str`.
34+
// When the function is called with these inputs,
35+
// Then it should return 1, indicating that 1 occurrences of `char` were found.
36+
test("it should return 1 because the character occurrences 1 time", () => {
37+
const str = "code your future";
38+
const char = "f";
39+
const count = countChar(str, char);
40+
expect(count).toEqual(1);
41+
});
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1-
function getOrdinalNumber(num) {
2-
return "1st";
1+
function getOrdinalNumber(number) {
2+
const lastTwoDigits = number % 100;
3+
if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) {
4+
return number + "th";
5+
}
6+
const lastDigit = number % 10;
7+
let suffix;
8+
if (lastDigit === 1) {
9+
suffix = "st";
10+
} else if (lastDigit === 2) {
11+
suffix = "nd";
12+
} else if (lastDigit === 3) {
13+
suffix = "rd";
14+
} else {
15+
suffix = "th";
16+
}
17+
18+
return number + suffix;
319
}
420

521
module.exports = getOrdinalNumber;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,27 @@ 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+
// Case 1: Numbers ending with 1 (but not 11)
22+
// When the number ends with 1, except those ending with 11,
23+
// Then the function should return a string by appending "st" to the number.
24+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
25+
expect(getOrdinalNumber(2)).toEqual("2nd");
26+
expect(getOrdinalNumber(42)).toEqual("42nd");
27+
expect(getOrdinalNumber(1752)).toEqual("1752nd");
28+
});
29+
// Case 1: Numbers ending with 1 (but not 11)
30+
// When the number ends with 1, except those ending with 11,
31+
// Then the function should return a string by appending "st" to the number.
32+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
33+
expect(getOrdinalNumber(3)).toEqual("3rd");
34+
expect(getOrdinalNumber(83)).toEqual("83rd");
35+
expect(getOrdinalNumber(653)).toEqual("653rd");
36+
});
37+
// Case 1: Numbers ending with 1 (but not 11)
38+
// When the number ends with 1, except those ending with 11,
39+
// Then the function should return a string by appending "st" to the number.
40+
test("should append 'th' for numbers not ending with 1, 2 and 3 ", () => {
41+
expect(getOrdinalNumber(5)).toEqual("5th");
42+
expect(getOrdinalNumber(38)).toEqual("38th");
43+
expect(getOrdinalNumber(15678)).toEqual("15678th");
44+
});
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(word, times) {
2+
if (times > 0) {
3+
return word.repeat(times);
4+
} else if (times < 0) {
5+
return "Error:negative number not allowed";
6+
} else {
7+
return "";
8+
}
39
}
4-
510
module.exports = repeatStr;

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,39 @@ const repeatStr = require("./repeat-str");
1010
// Then it should return a string that contains the original `str` repeated `count` times.
1111

1212
test("should repeat the string count times", () => {
13-
const str = "hello";
14-
const count = 3;
15-
const repeatedStr = repeatStr(str, count);
13+
let word = "hello";
14+
let times = 3;
15+
const repeatedStr = repeatStr(word, times);
1616
expect(repeatedStr).toEqual("hellohellohello");
1717
});
1818

1919
// Case: handle count of 1:
2020
// Given a target string `str` and a `count` equal to 1,
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
23-
23+
test("should repeat the string count times", () => {
24+
let word = "code your future";
25+
let times = 1;
26+
const repeatedStr = repeatStr(word, times);
27+
expect(repeatedStr).toEqual("code your future");
28+
});
2429
// Case: Handle count of 0:
2530
// Given a target string `str` and a `count` equal to 0,
2631
// When the repeatStr function is called with these inputs,
2732
// Then it should return an empty string.
28-
33+
test("should repeat the string is empty ", () => {
34+
let word = "hello";
35+
let times = 0;
36+
const repeatedStr = repeatStr(word, times);
37+
expect(repeatedStr).toBe("");
38+
});
2939
// Case: Handle negative count:
3040
// Given a target string `str` and a negative integer `count`,
3141
// When the repeatStr function is called with these inputs,
3242
// Then it should throw an error, as negative counts are not valid.
43+
test("should repeat the string return negative number not allowed ", () => {
44+
let word = "hello";
45+
let times = -3;
46+
const repeatedStr = repeatStr(word, times);
47+
expect(repeatedStr).toBe("Error:negative number not allowed");
48+
});

0 commit comments

Comments
 (0)