Skip to content

Commit 3167d4f

Browse files
committed
writing test for the given function and writing a function by test driven development
1 parent efaa7b0 commit 3167d4f

6 files changed

Lines changed: 95 additions & 6 deletions

File tree

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,39 @@ test("should count multiple occurrences of a character", () => {
1616
const count = countChar(str, char);
1717
expect(count).toEqual(5);
1818
});
19+
test("should count multiple occurrences of a character", () => {
20+
const str = "hello";
21+
const char = "l";
22+
const count = countChar(str, char);
23+
expect(count).toEqual(2);
24+
});
25+
test("should count multiple occurrences of a character", () => {
26+
const str = "speed";
27+
const char = "e";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(2);
30+
});
1931

2032
// Scenario: No Occurrences
2133
// Given the input string `str`,
2234
// And a character `char` that does not exist within `str`.
2335
// When the function is called with these inputs,
2436
// Then it should return 0, indicating that no occurrences of `char` were found.
37+
test("should count multiple occurrences of a character", () => {
38+
const str = "kkkk";
39+
const char = "l";
40+
const count = countChar(str, char);
41+
expect(count).toEqual(0);
42+
});
43+
test("should count multiple occurrences of a character", () => {
44+
const str = "bbbb";
45+
const char = "c";
46+
const count = countChar(str, char);
47+
expect(count).toEqual(0);
48+
});
49+
test("should count multiple occurrences of a character", () => {
50+
const str = "mmmmmzmze";
51+
const char = "d";
52+
const count = countChar(str, char);
53+
expect(count).toEqual(0);
54+
});
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 numberValue = num;
3+
let lastDigit = numberValue % 10;
4+
let lastTwoDigit = numberValue % 100;
5+
if (lastTwoDigit === 11 || lastTwoDigit === 12 || lastTwoDigit === 13) {
6+
return String(numberValue) + "th";
7+
}
8+
if (lastDigit === 1) {
9+
return String(numberValue) + "st";
10+
}
11+
if (lastDigit === 2) {
12+
return String(numberValue) + "nd";
13+
}
14+
if (lastDigit === 3) {
15+
return String(numberValue) + "rd";
16+
} else return String(numberValue) + "th";
317
}
418

519
module.exports = getOrdinalNumber;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,19 @@ 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 2: Numbers ending with 2 (but not 12)
22+
// When the number ends with 2, except those ending with 12,
23+
// Then the function should return a string by appending "nd" 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(22)).toEqual("22nd");
27+
expect(getOrdinalNumber(52)).toEqual("52nd");
28+
});
29+
// Case 3: Numbers ending with 3 (but not 13)
30+
// When the number ends with 3, except those ending with 13,
31+
// Then the function should return a string by appending "rd" 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(33)).toEqual("33rd");
35+
expect(getOrdinalNumber(133)).toEqual("133rd");
36+
});
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
function repeatStr() {
1+
function repeatStr(str, num) {
2+
let result = "";
23
// 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).
34
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
5+
for (let i = 0; i < num; i++) {
6+
result += str;
7+
}
8+
if (num > 0) {
9+
return result;
10+
} else if (num === 0) {
11+
return "";
12+
} else throw new error();
513
}
614

715
module.exports = repeatStr;

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,28 @@ test("should repeat the string count times", () => {
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 return the original string when count equal to 1", () => {
24+
const str = "manchester";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
expect(repeatedStr).toEqual("manchester");
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 return the empty string when count equal to 0", () => {
34+
const str = "ahmed";
35+
const count = 0;
36+
const repeatedStr = repeatStr(str, count);
37+
expect(repeatedStr).toEqual("");
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 throw an error when negative values pass to it", () => {
44+
expect(() => {
45+
repeatedStr("hi", -3);
46+
}).toThrow();
47+
});

0 commit comments

Comments
 (0)