-
-
Notifications
You must be signed in to change notification settings - Fork 335
Scotland | ITP JAN-2026 | Tuan Nguyen | Sprint 3 | 2- Practice-tdd #1258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
a00047b
c9d6085
e589ff5
1063a4d
7b67103
3982566
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,33 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| function countChar(fullString,findCharacters) { | ||
| let count = 0; | ||
| // both of these if statement is for input validation if the input not a string. | ||
| if( typeof findCharacters !== "string" || typeof fullString !== "string"){ | ||
| throw new Error("Invalid input"); | ||
| }; | ||
|
|
||
| if (findCharacters.length < 1) { | ||
| throw new Error("findCharacters must be a single character"); | ||
| } | ||
|
|
||
| for (let i = 0; i < fullString.length; i++){ | ||
| if(fullString[i] === findCharacters){ | ||
| count++; | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| module.exports = countChar; | ||
|
|
||
|
|
||
| function assertTest(testInput,testCheck){ | ||
| console.assert( | ||
| testInput === testCheck, | ||
| `Expect ${testInput} equal to ${testCheck}` | ||
| ); | ||
| }; | ||
|
|
||
| assertTest(countChar("whale fat hat cat","a"),4) | ||
| assertTest(countChar("I need to lean more and know more","e"),5) | ||
| assertTest(countChar("the city centre currently have a carnival","c"),4) | ||
| assertTest(countChar("the cruise ship in in transit to south America","s"),4) | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,50 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| // This is for num argument validation that go inside the function. | ||
| if(typeof num !== "number" || Number.isNaN(num)){ | ||
| throw new Error ("Invalid input: the value must be a number"); | ||
| } | ||
|
|
||
| // This allow float number to be round into it nearest integer. | ||
| num = Math.round(num); | ||
cjyuan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if(num % 100 === 11 ||num % 100 === 12 || num % 100 === 13 ){ | ||
| return `${num}th`; | ||
| } | ||
| switch( true ){ | ||
| case num % 10 === 1 : return `${num}st`; | ||
| case num % 10 === 2 : return `${num}nd`; | ||
| case num % 10 === 3 : return `${num}rd`; | ||
| default : return `${num}th`; | ||
| } | ||
cjyuan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; | ||
|
|
||
|
|
||
| function testAssert (inputNumber,outputNUmber){ | ||
| console.assert( | ||
| inputNumber === outputNUmber, | ||
| `Test failed: expected ${outputNUmber}, but got ${inputNumber}` | ||
| ); | ||
| }; | ||
| //Basic test of first digit integer | ||
| testAssert(getOrdinalNumber(1), "1st"); | ||
| testAssert(getOrdinalNumber(2), "2nd"); | ||
| testAssert(getOrdinalNumber(3), "3rd"); | ||
| testAssert(getOrdinalNumber(4), "4th"); | ||
| //Test for 11, 12 and 13 | ||
| testAssert(getOrdinalNumber(11), "11th"); | ||
| testAssert(getOrdinalNumber(12), "12th"); | ||
| testAssert(getOrdinalNumber(13), "13th"); | ||
| //Test for double digit number | ||
| testAssert(getOrdinalNumber(21), "21st"); | ||
| testAssert(getOrdinalNumber(32), "32nd"); | ||
| testAssert(getOrdinalNumber(43), "43rd"); | ||
| //Normal number and float number test. | ||
| testAssert(getOrdinalNumber(101), "101st"); | ||
| testAssert(getOrdinalNumber(202), "202nd"); | ||
| testAssert(getOrdinalNumber(1.2), "1st"); | ||
| testAssert(getOrdinalNumber(10.51), "11th"); | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,64 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" | |
| expect(getOrdinalNumber(21)).toEqual("21st"); | ||
| expect(getOrdinalNumber(131)).toEqual("131st"); | ||
| }); | ||
|
|
||
| // Case 2: Numbers ending with 2 (but not 12) | ||
| // When the number ends with 2, except those ending with 12, | ||
| // Then the function should return a string by appending "nd". | ||
| test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { | ||
| expect(getOrdinalNumber(2)).toEqual("2nd"); | ||
| expect(getOrdinalNumber(22)).toEqual("22nd"); | ||
| expect(getOrdinalNumber(142)).toEqual("142nd"); | ||
| }); | ||
|
|
||
| // Case 3: Numbers ending with 3 (but not 13) | ||
| // When the number ends with 3, except those ending with 13, | ||
| // Then the function should return a string by appending 'rd'. | ||
| test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { | ||
| expect(getOrdinalNumber(3)).toEqual("3rd"); | ||
| expect(getOrdinalNumber(33)).toEqual("33rd"); | ||
| expect(getOrdinalNumber(153)).toEqual("153rd"); | ||
| }); | ||
|
|
||
| // Case 4: Special cases 11, 12, 13 | ||
| // When the number ends with 11, 12, or 13, | ||
| // Then the function should always append "th". | ||
| test("should append 'th' for special cases 11, 12, 13", () => { | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| expect(getOrdinalNumber(12)).toEqual("12th"); | ||
| expect(getOrdinalNumber(13)).toEqual("13th"); | ||
| expect(getOrdinalNumber(111)).toEqual("111th"); | ||
| expect(getOrdinalNumber(212)).toEqual("212th"); | ||
| }); | ||
|
|
||
| // Case 5: All other numbers | ||
| // When the number does not end with 1, 2, or 3, | ||
| // Then the function should append "th". | ||
| test("should append 'th' for all other numbers", () => { | ||
|
||
| expect(getOrdinalNumber(4)).toEqual("4th"); | ||
| expect(getOrdinalNumber(10)).toEqual("10th"); | ||
| expect(getOrdinalNumber(100)).toEqual("100th"); | ||
| expect(getOrdinalNumber(204)).toEqual("204th"); | ||
| }); | ||
|
|
||
| // Case 6: Float numbers should be rounded to nearest integer | ||
| // When the input is a float, | ||
| // Then the function should round it and return the correct ordinal. | ||
| test("should round float numbers and return correct ordinal", () => { | ||
| expect(getOrdinalNumber(1.2)).toEqual("1st"); // rounds to 1 | ||
| expect(getOrdinalNumber(1.8)).toEqual("2nd"); // rounds to 2 | ||
| expect(getOrdinalNumber(2.5)).toEqual("3rd"); // rounds to 3 | ||
| expect(getOrdinalNumber(10.51)).toEqual("11th"); // rounds to 11 | ||
| expect(getOrdinalNumber(12.49)).toEqual("12th"); // rounds to 12 | ||
| expect(getOrdinalNumber(12.5)).toEqual("13th"); // rounds to 13 | ||
| }); | ||
|
|
||
| // Case 7: Invalid inputs should throw an error | ||
| // When the input is not a number, | ||
| // Then the function should throw an error. | ||
| test("should throw an error for invalid inputs", () => { | ||
| expect(() => getOrdinalNumber("10")).toThrow("Invalid input"); | ||
| expect(() => getOrdinalNumber(null)).toThrow("Invalid input"); | ||
| expect(() => getOrdinalNumber(undefined)).toThrow("Invalid input"); | ||
| expect(() => getOrdinalNumber(NaN)).toThrow("Invalid input"); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.