-
-
Notifications
You must be signed in to change notification settings - Fork 336
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 all 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) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,51 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| // This is for num argument validation that go inside the function. | ||
| if ( !Number.isInteger(num)) { | ||
| throw new Error("Invalid input: the value must be a number"); | ||
| } | ||
|
|
||
| // This allow float number to be round into it nearest integer. | ||
| const mainReminder = num % 100; | ||
| const secondReminder = num % 10; | ||
|
|
||
| if (mainReminder === 11 || mainReminder === 12 || mainReminder === 13) { | ||
| return `${num}th`; | ||
| } | ||
| switch (true) { | ||
| case secondReminder === 1: | ||
| return `${num}st`; | ||
| case secondReminder === 2: | ||
| return `${num}nd`; | ||
| case secondReminder === 3: | ||
| return `${num}rd`; | ||
| default: | ||
| return `${num}th`; | ||
| } | ||
| } | ||
|
|
||
| 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), "Invalid input"); | ||
| testAssert(() => getOrdinalNumber(10.51), "Invalid input"); |
| 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 that is not end in 1,2 or 3. | ||
| // When the number does not end with 1, 2, or 3, | ||
| // Then the function should append "th". | ||
| test("should append 'th' for all number that don't end in 1,2 or 3", () => { | ||
| 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 giving a error since the function do nut accept float number input | ||
| // When the input is a float, | ||
| // Then the function should round it and return the correct ordinal. | ||
| test("should return error for invalid input", () => { | ||
| expect(() => getOrdinalNumber(1.2)).toThrow("Invalid input"); | ||
| expect(() => getOrdinalNumber(1.8)).toThrow("Invalid input"); | ||
| expect(() => getOrdinalNumber(2.5)).toThrow("Invalid input"); | ||
| expect(() => getOrdinalNumber(10.51)).toThrow("Invalid input"); | ||
| expect(() => getOrdinalNumber(12.49)).toThrow("Invalid input"); | ||
| expect(() => getOrdinalNumber(12.5)).toThrow("Invalid input"); | ||
|
Comment on lines
+65
to
+70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why test so many decimal numbers with decimal point? If the objective is to show the function should treat any number with decimal points as invalid input, one or two samples should be enough. Under the "invalid input" category, it would probably be more useful to show what other kinds of input are considered invalid. For examples,
|
||
| }); | ||
|
|
||
| // 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.