Skip to content

Commit 8507a87

Browse files
built up the programs starting form the test cases to validate the functions
1 parent 3372770 commit 8507a87

File tree

6 files changed

+108
-5
lines changed

6 files changed

+108
-5
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
//counts the number of times a character occurs in a string
12
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
3+
let count =0;
4+
for (let i = 0; i < stringOfCharacters.length; i++) {
5+
if (stringOfCharacters[i] === findCharacter) {
6+
count+=1;
7+
}
8+
}
9+
10+
11+
return count
12+
313
}
414

515
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
@@ -3,6 +3,11 @@ const countChar = require("./count");
33
// Given a string `str` and a single character `char` to search for,
44
// When the countChar function is called with these inputs,
55
// Then it should:
6+
test("should count the number of times a character occurs in a string", () => {
7+
expect(countChar('hello world', "o")).toEqual(2);
8+
expect(countChar('hello world', "l")).toEqual(3);
9+
expect(countChar('hello world', "z")).toEqual(0);
10+
});
611

712
// Scenario: Multiple Occurrences
813
// Given the input string `str`,
@@ -22,3 +27,10 @@ test("should count multiple occurrences of a character", () => {
2227
// And a character `char` that does not exist within `str`.
2328
// When the function is called with these inputs,
2429
// Then it should return 0, indicating that no occurrences of `char` were found.
30+
31+
test("should return 0 when the character does not accur in the string", () => {
32+
const str = "hello world";
33+
const char = "z";
34+
const count = countChar(str, char);
35+
expect(count).toEqual(0);
36+
});
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
let lastTwoDigits = num % 100;
3+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
4+
return num + "th";
5+
}
6+
let lastDigit = num % 10;
7+
if (lastDigit === 1) {
8+
return num + "st";
9+
}
10+
if (lastDigit === 2) {
11+
return num + 'nd';
12+
}
13+
if (lastDigit === 3) {
14+
return num + 'rd';
15+
}
16+
return num + 'th';
317
}
418

519
module.exports = getOrdinalNumber;

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,39 @@ 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+
// Case 2: Numbers ending with 2 (but not 12)
23+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
24+
expect(getOrdinalNumber(2)).toEqual("2nd");
25+
expect(getOrdinalNumber(22)).toEqual("22nd");
26+
expect(getOrdinalNumber(132)).toEqual("132nd");
27+
});
28+
29+
// Case 3: Numbers ending with 3 (but not 13)
30+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
31+
expect(getOrdinalNumber(3)).toEqual("3rd");
32+
expect(getOrdinalNumber(23)).toEqual("23rd");
33+
expect(getOrdinalNumber(133)).toEqual("133rd");
34+
});
35+
36+
// Case 4: Numbers ending with 11, 12 or 13
37+
test( "should append 'th' for numbers ending with 11, 12 or 13", ()=> {
38+
expect(getOrdinalNumber(11)).toEqual('11th');
39+
expect(getOrdinalNumber(12)).toEqual('12th');
40+
expect(getOrdinalNumber(13)).toEqual('13th');
41+
expect(getOrdinalNumber(111)).toEqual('111th');
42+
expect(getOrdinalNumber(112)).toEqual('112th');
43+
expect(getOrdinalNumber(113)).toEqual('113th');
44+
})
45+
46+
//Case 5: Numbers ending with 0, 4, 5, 6, 7, 8 or 9
47+
test("should append 'th' for numbers ending with 0, 4, 5, 6, 7, 8, or 9", () => {
48+
expect(getOrdinalNumber(10)).toEqual("10th");
49+
expect(getOrdinalNumber(4)).toEqual("4th");
50+
expect(getOrdinalNumber(5)).toEqual("5th");
51+
expect(getOrdinalNumber(6)).toEqual("6th");
52+
expect(getOrdinalNumber(7)).toEqual("7th");
53+
expect(getOrdinalNumber(8)).toEqual("8th");
54+
expect(getOrdinalNumber(9)).toEqual("9th");
55+
expect(getOrdinalNumber(100)).toEqual("100th");
56+
});
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count must be a non-negative integer");
4+
}
5+
6+
if (count === 0) {
7+
return "";
8+
}
9+
10+
let repeatedStr = "";
11+
12+
for (let i = 0; i < count; i++) {
13+
repeatedStr += str;
14+
}
15+
16+
return repeatedStr;
317
}
418

519
module.exports = repeatStr;

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,29 @@ 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 return the original string when count is 1", () => {
25+
const str = "world";
26+
const count = 1;
27+
const repeatedStr = repeatStr(str, count);
28+
expect(repeatedStr).toEqual("world");
29+
});
2430
// Case: Handle count of 0:
2531
// Given a target string `str` and a `count` equal to 0,
2632
// When the repeatStr function is called with these inputs,
2733
// Then it should return an empty string.
28-
34+
test ("should return an empty string when count is 0", () => {
35+
const str = "test";
36+
const count = 0;
37+
const repeatedStr = repeatStr(str, count);
38+
expect(repeatedStr).toEqual("");
39+
});
2940
// Case: Handle negative count:
3041
// Given a target string `str` and a negative integer `count`,
3142
// When the repeatStr function is called with these inputs,
3243
// Then it should throw an error, as negative counts are not valid.
44+
45+
test ("should throw an error when count is negative", ()=> {
46+
const str = "error";
47+
const count = -1;
48+
expect(() => repeatStr(str, count)).toThrow("Count must be a non-negative integer");
49+
})

0 commit comments

Comments
 (0)