Skip to content

Commit e589ff5

Browse files
Complete the rest course work of this branch to be submit for PR
1 parent c9d6085 commit e589ff5

File tree

4 files changed

+152
-4
lines changed

4 files changed

+152
-4
lines changed
Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,50 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
// This is for num argument validation that go inside the function.
3+
if(typeof num !== "number" || Number.isNaN(num)){
4+
throw new Error ("Invalid input: the value must be a number");
5+
}
6+
7+
// This allow float number to be round into it nearest integer.
8+
num = Math.round(num);
9+
10+
if(num % 100 === 11 ||num % 100 === 12 || num % 100 === 13 ){
11+
return `${num}th`;
12+
}
13+
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`;
17+
default : return `${num}th`;
18+
}
319
}
420

521
module.exports = getOrdinalNumber;
22+
23+
24+
function testAssert (inputNumber,outputNUmber){
25+
console.assert(
26+
inputNumber === outputNUmber,
27+
`Test failed: expected ${outputNUmber}, but got ${inputNumber}`
28+
);
29+
};
30+
//Basic test of first digit integer
31+
testAssert(getOrdinalNumber(1), "1st");
32+
testAssert(getOrdinalNumber(2), "2nd");
33+
testAssert(getOrdinalNumber(3), "3rd");
34+
testAssert(getOrdinalNumber(4), "4th");
35+
//Test for 11, 12 and 13
36+
testAssert(getOrdinalNumber(11), "11th");
37+
testAssert(getOrdinalNumber(12), "12th");
38+
testAssert(getOrdinalNumber(13), "13th");
39+
//Test for double digit number
40+
testAssert(getOrdinalNumber(21), "21st");
41+
testAssert(getOrdinalNumber(32), "32nd");
42+
testAssert(getOrdinalNumber(43), "43rd");
43+
//Normal number and float number test.
44+
testAssert(getOrdinalNumber(101), "101st");
45+
testAssert(getOrdinalNumber(202), "202nd");
46+
testAssert(getOrdinalNumber(1.2), "1st");
47+
testAssert(getOrdinalNumber(10.51), "11th");
48+
49+
50+

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,64 @@ 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+
// When the number ends with 2, except those ending with 12,
24+
// Then the function should return a string by appending "nd".
25+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
26+
expect(getOrdinalNumber(2)).toEqual("2nd");
27+
expect(getOrdinalNumber(22)).toEqual("22nd");
28+
expect(getOrdinalNumber(142)).toEqual("142nd");
29+
});
30+
31+
// Case 3: Numbers ending with 3 (but not 13)
32+
// When the number ends with 3, except those ending with 13,
33+
// Then the function should return a string by appending 'rd'.
34+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
35+
expect(getOrdinalNumber(3)).toEqual("3rd");
36+
expect(getOrdinalNumber(33)).toEqual("33rd");
37+
expect(getOrdinalNumber(153)).toEqual("153rd");
38+
});
39+
40+
// Case 4: Special cases 11, 12, 13
41+
// When the number ends with 11, 12, or 13,
42+
// Then the function should always append "th".
43+
test("should append 'th' for special cases 11, 12, 13", () => {
44+
expect(getOrdinalNumber(11)).toEqual("11th");
45+
expect(getOrdinalNumber(12)).toEqual("12th");
46+
expect(getOrdinalNumber(13)).toEqual("13th");
47+
expect(getOrdinalNumber(111)).toEqual("111th");
48+
expect(getOrdinalNumber(212)).toEqual("212th");
49+
});
50+
51+
// Case 5: All other numbers
52+
// When the number does not end with 1, 2, or 3,
53+
// Then the function should append "th".
54+
test("should append 'th' for all other numbers", () => {
55+
expect(getOrdinalNumber(4)).toEqual("4th");
56+
expect(getOrdinalNumber(10)).toEqual("10th");
57+
expect(getOrdinalNumber(100)).toEqual("100th");
58+
expect(getOrdinalNumber(204)).toEqual("204th");
59+
});
60+
61+
// Case 6: Float numbers should be rounded to nearest integer
62+
// When the input is a float,
63+
// 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
71+
});
72+
73+
// Case 7: Invalid inputs should throw an error
74+
// When the input is not a number,
75+
// Then the function should throw an error.
76+
test("should throw an error for invalid inputs", () => {
77+
expect(() => getOrdinalNumber("10")).toThrow("Invalid input");
78+
expect(() => getOrdinalNumber(null)).toThrow("Invalid input");
79+
expect(() => getOrdinalNumber(undefined)).toThrow("Invalid input");
80+
expect(() => getOrdinalNumber(NaN)).toThrow("Invalid input");
81+
});
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1-
function repeatStr() {
2-
return "hellohellohello";
3-
}
1+
function repeatStr(fullString,repeatCount) {
2+
if( repeatCount < 0){
3+
throw new Error("repeatCount cannot be negative")
4+
};
5+
6+
switch (true){
7+
case repeatCount === 0 : return "";
8+
default: return fullString.repeat(repeatCount) ;
9+
};
10+
};
411

512
module.exports = repeatStr;
13+
14+
function assertTest(testInput,testOutput){
15+
console.assert(
16+
testInput === testOutput,
17+
`Expect ${testOutput} instead of ${testInput}`
18+
)
19+
}
20+
21+
assertTest(repeatStr("left",3),"leftleftleft")
22+
assertTest(repeatStr("learn",1),"learn")
23+
assertTest(repeatStr("zero",0),"")
24+
assertTest(repeatStr("negative",-1),"repeatCount cannot be negative")

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,35 @@ 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+
// Case 2: handle count of 1
25+
test("returns the original string when count is 1", () => {
26+
const str = "hello";
27+
const count = 1;
28+
const result = repeatStr(str, count);
29+
expect(result).toBe("hello");
30+
});
31+
2432
// Case: Handle count of 0:
2533
// Given a target string `str` and a `count` equal to 0,
2634
// When the repeatStr function is called with these inputs,
2735
// Then it should return an empty string.
2836

37+
// Case 3: handle count of 0
38+
test("returns an empty string when count is 0", () => {
39+
const str = "hello";
40+
const count = 0;
41+
const result = repeatStr(str, count);
42+
expect(result).toBe("");
43+
});
44+
2945
// Case: Handle negative count:
3046
// Given a target string `str` and a negative integer `count`,
3147
// When the repeatStr function is called with these inputs,
3248
// Then it should throw an error, as negative counts are not valid.
49+
50+
// Case 4: handle negative count
51+
test("throws an error when count is negative", () => {
52+
const str = "hello";
53+
const count = -2;
54+
expect(() => repeatStr(str, count)).toThrow("repeatCount cannot be negative");
55+
});

0 commit comments

Comments
 (0)