-
-
Notifications
You must be signed in to change notification settings - Fork 238
London | 25-ITP-Sep | Adnaan Abo | Sprint 3 | coursework/sprint-3-practice-tdd #718
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 8 commits
889f34e
c40ecef
f097ae0
09a628b
a87356b
8b8e22f
b31f116
daf6efa
c7bc598
4183f3b
c3eae64
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,12 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| function countChar(str, char) { | ||
| if (!char || char.length !== 1) return 0; // ensure char is a single character | ||
| let count = 0; | ||
| for (let i = 0; i < str.length; i++) { | ||
| if (str[i] === char) { | ||
| count++; | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| module.exports = countChar; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,21 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| if (!Number.isInteger(num) || num <= 0) { | ||
| return "Invalid input"; | ||
| } | ||
| if (num % 100 >= 11 && num % 100 <= 13) { | ||
| return num + "th"; | ||
| } | ||
| switch (num % 10) { | ||
| case 1: | ||
| return num + "st"; | ||
| case 2: | ||
| return num + "nd"; | ||
| case 3: | ||
| return num + "rd"; | ||
| default: | ||
| return num + "th"; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,59 @@ const getOrdinalNumber = require("./get-ordinal-number"); | |
| // When the number is 1, | ||
| // Then the function should return "1st" | ||
|
|
||
| test("should return '1st' for 1", () => { | ||
| // category 1: ends with 1 (but not 11) -> "st" | ||
| test("append 'st' to numbers ending in 1, except those ending in 11", () => { | ||
| expect(getOrdinalNumber(1)).toEqual("1st"); | ||
| expect(getOrdinalNumber(21)).toEqual("21st"); | ||
| expect(getOrdinalNumber(101)).toEqual("101st"); | ||
| }); | ||
|
|
||
| // Case 2: Identify the ordinal number for 2 | ||
| // When the number is 2, | ||
| // Then the function should return "2nd" | ||
|
|
||
| // category 2: ends with 2 (but not 12) -> "nd" | ||
| test("append 'nd' to numbers ending in 2, except those ending in 12", () => { | ||
| expect(getOrdinalNumber(2)).toEqual("2nd"); | ||
| expect(getOrdinalNumber(22)).toEqual("22nd"); | ||
| expect(getOrdinalNumber(102)).toEqual("102nd"); | ||
| }); | ||
|
|
||
| // Case 3: Identify the ordinal number for 3 | ||
| // When the number is 3, | ||
| // Then the function should return "3rd" | ||
|
|
||
| // category 3: ends with 3 (but not 13) -> "rd" | ||
| test("append 'rd' to numbers ending in 3, except those ending in 13", () => { | ||
| expect(getOrdinalNumber(3)).toEqual("3rd"); | ||
| expect(getOrdinalNumber(23)).toEqual("23rd"); | ||
| expect(getOrdinalNumber(103)).toEqual("103rd"); | ||
| }); | ||
|
|
||
| // Case 4: Identify the ordinal number for 4 | ||
| // When the number is 4, | ||
| // Then the function should return "4th" | ||
|
|
||
| // category 4: all other numbers -> "th" | ||
| test("append 'th' to all other numbers", () => { | ||
| expect(getOrdinalNumber(4)).toEqual("4th"); | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| expect(getOrdinalNumber(12)).toEqual("12th"); | ||
| expect(getOrdinalNumber(13)).toEqual("13th"); | ||
| expect(getOrdinalNumber(14)).toEqual("14th"); | ||
| expect(getOrdinalNumber(111)).toEqual("111th"); | ||
| expect(getOrdinalNumber(112)).toEqual("112th"); | ||
| expect(getOrdinalNumber(113)).toEqual("113th"); | ||
|
Comment on lines
+49
to
+54
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 the same numbers again in a separate test? 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. You are right all the test under category 5: special case for 11, 12, 13 -> "th" can be removed since they are tested in category 4: all other numbers -> "th" |
||
| expect(getOrdinalNumber(114)).toEqual("114th"); | ||
| }); | ||
|
|
||
| // category 5: special case for 11, 12, 13 -> "th" | ||
| test("append 'th' to numbers ending in 11, 12, or 13", () => { | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| expect(getOrdinalNumber(12)).toEqual("12th"); | ||
| expect(getOrdinalNumber(13)).toEqual("13th"); | ||
| expect(getOrdinalNumber(111)).toEqual("111th"); | ||
| expect(getOrdinalNumber(112)).toEqual("112th"); | ||
| expect(getOrdinalNumber(113)).toEqual("113th"); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,30 @@ | ||
| function repeat() { | ||
| return "hellohellohello"; | ||
| function repeat(str, count) { | ||
| if (arguments.length !== 2) { | ||
| throw new Error("Function requires exactly two arguments: str and count."); | ||
| } | ||
|
|
||
| if (typeof str !== "string") { | ||
| throw new Error("First argument must be a string."); | ||
| } | ||
|
|
||
| if (!Number.isSafeInteger(count) || count < 0) { | ||
| throw new Error("Second argument must be a non-negative integer."); | ||
| } | ||
|
|
||
| if (count === 0) { | ||
| return ""; | ||
| } | ||
|
|
||
| if (count === 1) { | ||
| return str; | ||
| } | ||
|
|
||
| let result = ""; | ||
| for (let i = 0; i < count; i++) { | ||
| result += str; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
| module.exports = repeat; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this test fails, the description "all other numbers" is not quite informative; one would need to read the other tests to figure out what numbers are considered as "other numbers".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed to more detailed description, "append 'th' to numbers ending in 0, 4–9, or 11–13"