Skip to content

Commit 1063a4d

Browse files
Making some major chang for PR review again
1 parent e589ff5 commit 1063a4d

File tree

5 files changed

+41
-24
lines changed

5 files changed

+41
-24
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function countChar(fullString,findCharacters) {
55
throw new Error("Invalid input");
66
};
77

8-
if (findCharacters.length < 1) {
8+
if (findCharacters.length !== 1) {
99
throw new Error("findCharacters must be a single character");
1010
}
1111

@@ -30,4 +30,4 @@ function assertTest(testInput,testCheck){
3030
assertTest(countChar("whale fat hat cat","a"),4)
3131
assertTest(countChar("I need to lean more and know more","e"),5)
3232
assertTest(countChar("the city centre currently have a carnival","c"),4)
33-
assertTest(countChar("the cruise ship in in transit to south America","s"),4)
33+
assertTest(countChar("the cruise ship in in transit to south America","s"),4)

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,23 @@ test("should count multiple occurrences of a character", () => {
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of `char` were found.
2525
test( "should return count of zero occurrences of a character", () =>{
26-
expect(countChar("go home and study","c")).toEqual(0)
26+
expect(countChar("go home and study","c")).toEqual(0);
27+
});
28+
29+
// Scenario: Case-sensitive matching
30+
// Given a string containing both uppercase and lowercase versions of a letter,
31+
// When the function is called with a lowercase character,
32+
// Then it should only count the lowercase occurrences.
33+
34+
test("should treat character matching as case-sensitive", () => {
35+
expect(countChar("AaAa","A")).toEqual(2);
36+
});
37+
38+
// Scenario: Non-alphabet characters
39+
// Given a string containing digits and symbols,
40+
// When the function is called with a non-alphabet character,
41+
// Then it should correctly count occurrences of that character.
42+
43+
test("should count occurrences of non-alphabet characters", () => {
44+
expect(countChar("1-2-3-1-1","1")).toEqual(3);
2745
});

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
function getOrdinalNumber(num) {
22
// This is for num argument validation that go inside the function.
3-
if(typeof num !== "number" || Number.isNaN(num)){
3+
if(typeof num !== "number" || Number.isNaN(num) || !Number.isInteger(num)){
44
throw new Error ("Invalid input: the value must be a number");
55
}
66

77
// This allow float number to be round into it nearest integer.
8-
num = Math.round(num);
8+
const mainReminder = num % 100;
9+
const secondReminder = num % 10;
910

10-
if(num % 100 === 11 ||num % 100 === 12 || num % 100 === 13 ){
11+
if(mainReminder === 11 ||mainReminder === 12 || mainReminder === 13 ){
1112
return `${num}th`;
1213
}
1314
switch( true ){
14-
case num % 10 === 1 : return `${num}st`;
15-
case num % 10 === 2 : return `${num}nd`;
16-
case num % 10 === 3 : return `${num}rd`;
15+
case secondReminder === 1 : return `${num}st`;
16+
case secondReminder=== 2 : return `${num}nd`;
17+
case secondReminder=== 3 : return `${num}rd`;
1718
default : return `${num}th`;
1819
}
1920
}
@@ -43,8 +44,8 @@ testAssert(getOrdinalNumber(43), "43rd");
4344
//Normal number and float number test.
4445
testAssert(getOrdinalNumber(101), "101st");
4546
testAssert(getOrdinalNumber(202), "202nd");
46-
testAssert(getOrdinalNumber(1.2), "1st");
47-
testAssert(getOrdinalNumber(10.51), "11th");
47+
testAssert(() =>getOrdinalNumber(1.2),"Invalid input" );
48+
testAssert(() =>getOrdinalNumber(10.51),"Invalid input");
4849

4950

5051

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ test("should append 'th' for special cases 11, 12, 13", () => {
4848
expect(getOrdinalNumber(212)).toEqual("212th");
4949
});
5050

51-
// Case 5: All other numbers
51+
// Case 5: All other numbers that is not end in 1,2 or 3.
5252
// When the number does not end with 1, 2, or 3,
5353
// Then the function should append "th".
5454
test("should append 'th' for all other numbers", () => {
@@ -58,16 +58,16 @@ test("should append 'th' for all other numbers", () => {
5858
expect(getOrdinalNumber(204)).toEqual("204th");
5959
});
6060

61-
// Case 6: Float numbers should be rounded to nearest integer
61+
// Case 6: Float numbers should be giving a error since the function do nut accept float number input
6262
// When the input is a float,
6363
// Then the function should round it and return the correct ordinal.
64-
test("should round float numbers and return correct ordinal", () => {
65-
expect(getOrdinalNumber(1.2)).toEqual("1st"); // rounds to 1
66-
expect(getOrdinalNumber(1.8)).toEqual("2nd"); // rounds to 2
67-
expect(getOrdinalNumber(2.5)).toEqual("3rd"); // rounds to 3
68-
expect(getOrdinalNumber(10.51)).toEqual("11th"); // rounds to 11
69-
expect(getOrdinalNumber(12.49)).toEqual("12th"); // rounds to 12
70-
expect(getOrdinalNumber(12.5)).toEqual("13th"); // rounds to 13
64+
test("should return error for invalid input", () => {
65+
expect(() =>getOrdinalNumber(1.2)).toThrow("Invalid input");
66+
expect(() =>getOrdinalNumber(1.8)).toThrow("Invalid input");
67+
expect(() =>getOrdinalNumber(2.5)).toThrow("Invalid input");
68+
expect(() =>getOrdinalNumber(10.51)).toThrow("Invalid input");
69+
expect(() =>getOrdinalNumber(12.49)).toThrow("Invalid input");
70+
expect(() =>getOrdinalNumber(12.5)).toThrow("Invalid input");
7171
});
7272

7373
// Case 7: Invalid inputs should throw an error

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ function repeatStr(fullString,repeatCount) {
33
throw new Error("repeatCount cannot be negative")
44
};
55

6-
switch (true){
7-
case repeatCount === 0 : return "";
8-
default: return fullString.repeat(repeatCount) ;
9-
};
6+
if (repeatCount === 0) return "";
7+
return fullString.repeat(repeatCount)
108
};
119

1210
module.exports = repeatStr;

0 commit comments

Comments
 (0)