Skip to content

Commit ce61aaf

Browse files
committed
chages are done for undified and test cases are updated
1 parent a8355cf commit ce61aaf

File tree

5 files changed

+107
-13
lines changed

5 files changed

+107
-13
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
function countChar(stringOfCharacters, findCharacter) {
2+
if (
3+
typeof stringOfCharacters !== "string" ||
4+
typeof findCharacter !== "string"
5+
) {
6+
return 0;
7+
}
8+
if (findCharacter.length !== 1) {
9+
return 0;
10+
}
11+
212
let count = 0;
313
for (let i = 0; i < stringOfCharacters.length; i++) {
414
if (stringOfCharacters[i] === findCharacter) {

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

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,57 @@ test("should return 0 when the character does not exist in the string", () => {
5151
expect(count).toEqual(0);
5252
});
5353

54-
test("should return 0 when the character does not exist in the string", () => {
55-
const str = "hello";
56-
const char = "z";
54+
test("should return 0 when the 'c' does not exist in string 'I dont have an apple'", () => {
55+
const str = "I dont have an apple";
56+
const char = "c";
5757
const count = countChar(str, char);
5858
expect(count).toEqual(0);
5959
});
6060

61-
test("should return 0 when the 'c' does not exist in string 'I dont have an apple'", () => {
62-
const str = "I dont have an apple";
63-
const char = "c";
61+
// Scenario: Find character is longer than the input string
62+
// Given a string shorter than the findCharacter (e.g., 'a' vs 'abc'),
63+
// When the function is called,
64+
// Then it should return 0 because a multi-character string cannot match a single character position.
65+
66+
test("should return 0 when findCharacter is longer than the input string", () => {
67+
const str = "a";
68+
const char = "abc";
69+
const count = countChar(str, char);
70+
expect(count).toEqual(0);
71+
});
72+
73+
// Scenario: Empty string input
74+
// Given an empty input string and a valid character,
75+
// When the function is called,
76+
// Then it should return 0 because there are no characters to search.
77+
78+
test("should return 0 when input string is empty", () => {
79+
const str = "";
80+
const char = "a";
81+
const count = countChar(str, char);
82+
expect(count).toEqual(0);
83+
});
84+
85+
// Scenario: Non-string input type
86+
// Given that one or both inputs are not strings (e.g., number, array),
87+
// When the function is called,
88+
// Then it should return 0 or handle the input gracefully without throwing an error.
89+
90+
test("should return 0 when inputs are not strings", () => {
91+
const str = 12345;
92+
const char = 1;
93+
const count = countChar(str, char);
94+
expect(count).toEqual(0);
95+
});
96+
97+
// Scenario: Empty findCharacter
98+
// Given a valid string but an empty findCharacter,
99+
// When the function is called,
100+
// Then it should return 0 because an empty search target is not valid.
101+
102+
test("should return 0 when findCharacter is an empty string", () => {
103+
const str = "hello";
104+
const char = "";
64105
const count = countChar(str, char);
65106
expect(count).toEqual(0);
66107
});
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
function getOrdinalNumber(number) {
2-
if (number % 100 >= 11 && number % 100 <= 13) {
2+
// Handle invalid inputs
3+
if (typeof number !== "number" || !Number.isFinite(number)) {
4+
return "Invalid input";
5+
}
6+
7+
// Handle zero or negative numbers (optional)
8+
if (number <= 0) {
39
return number + "th";
4-
} else {
5-
const lastdigit = number.toString().slice(-1);
10+
}
611

7-
if (lastdigit == 1) return number + "st";
8-
if (lastdigit == 2) return number + "nd";
9-
if (lastdigit == 3) return number + "rd";
12+
// Handle special 11–13 endings
13+
if (number % 100 >= 11 && number % 100 <= 13) {
1014
return number + "th";
1115
}
16+
17+
// Get last digit to determine suffix
18+
const lastDigit = number % 10;
19+
20+
if (lastDigit === 1) return number + "st";
21+
if (lastDigit === 2) return number + "nd";
22+
if (lastDigit === 3) return number + "rd";
23+
return number + "th";
1224
}
1325

1426
module.exports = getOrdinalNumber;

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,35 @@ test("should return '20th' for 20", () => {
6767
test("should return '23rd' for 23", () => {
6868
expect(getOrdinalNumber(23)).toEqual("23rd");
6969
});
70+
71+
// Case Undefind :
72+
// When the nymber is Undefind ,
73+
// Then the function should return "Invalid input"
74+
75+
test("should return 'Invalid input' when input is undefined", () => {
76+
expect(getOrdinalNumber(undefined)).toEqual("Invalid input");
77+
});
78+
79+
// Case null :
80+
// When the nymber is null ,
81+
// Then the function should return "Invalid input"
82+
83+
test("should return 'Invalid input' when input is null", () => {
84+
expect(getOrdinalNumber(null)).toEqual("Invalid input");
85+
});
86+
87+
// Case negetive number :
88+
// When the number is negetive number ,
89+
// Then the function should return "-1th"
90+
91+
test("should return '-1th' for -1", () => {
92+
expect(getOrdinalNumber(-1)).toEqual("-1th");
93+
});
94+
95+
// Case NaN input :
96+
// When the nymber is NaN ,
97+
// Then the function should return "Invalid input"
98+
99+
test("should return 'Invalid input' when input is NaN", () => {
100+
expect(getOrdinalNumber(NaN)).toEqual("Invalid input");
101+
});

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ function repeat(str, count) {
1818
return "";
1919
}
2020

21-
// 5. Repeat the string count times
2221
let result = "";
2322
for (let i = 0; i < count; i++) {
2423
result += str;

0 commit comments

Comments
 (0)