-
-
Notifications
You must be signed in to change notification settings - Fork 245
Manchester| 25-ITP-September | Mahtem T. Mengstu | Sprint 3 | Coursework/sprint 3 practice tdd #874
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 6 commits
4b39af7
826a780
a1a791e
5b62a72
b3404b7
452b1a6
eb7c0eb
4b42c9f
6993bb1
7cf20f2
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,23 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| if (typeof stringOfCharacters!= "string") { | ||
| throw new Error("Input should be a string"); | ||
| } | ||
| if (typeof findCharacter !== "string" || findCharacter.length != 1){ | ||
| throw new Error ("Input must be a single character"); | ||
| } | ||
|
|
||
| let count = 0; | ||
| for(let char of stringOfCharacters) { | ||
| if (char === findCharacter) { | ||
| count++; | ||
| } | ||
| } | ||
| return count; | ||
|
|
||
| } | ||
|
|
||
| module.exports = countChar; | ||
|
|
||
| console.log(countChar("amazon", "a")); | ||
|
|
||
| // Added lines to check cases and return count of characters. | ||
|
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. Test is quite comprehensive. Can your function pass all the tests?
Author
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.
I have added, some more tests and the function passed all tests. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,25 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| if (!Number.isInteger(num) || num <= 0) { | ||
| throw new Error("Only positive integers are allowed"); | ||
| } | ||
| // Handle Exception of 11, 12, 13 | ||
| if (num % 100 >= 11 && num % 100 <= 13) { | ||
| return num + "th"; | ||
| } | ||
| // Handle general last-digit cases | ||
| 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; | ||
|
|
||
| // Function implemented |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,39 @@ const getOrdinalNumber = require("./get-ordinal-number"); | |
| // When the number is 1, | ||
| // Then the function should return "1st" | ||
|
|
||
| test("should return '1st' for 1", () => { | ||
| // Case 1: 1 → Standard ordinals | ||
|
|
||
| test("should return correct ordinals for 1, 2, 3, 4, 21, 22, 23, 24", () => { | ||
| expect(getOrdinalNumber(1)).toEqual("1st"); | ||
| expect(getOrdinalNumber(2)).toEqual("2nd"); | ||
| expect(getOrdinalNumber(3)).toEqual("3rd"); | ||
| expect(getOrdinalNumber(4)).toEqual("4th"); | ||
| expect(getOrdinalNumber(21)).toEqual("21st"); | ||
| expect(getOrdinalNumber(22)).toEqual("22nd"); | ||
| expect(getOrdinalNumber(23)).toEqual("23rd"); | ||
| expect(getOrdinalNumber(24)).toEqual("24th"); | ||
| }); | ||
|
|
||
| // Case 2: Exceptions 11, 12, 13 | ||
| test("should handle exceptions 11, 12, 13 correctly", () => { | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| expect(getOrdinalNumber(12)).toEqual("12th"); | ||
| expect(getOrdinalNumber(13)).toEqual("13th"); | ||
| expect(getOrdinalNumber(111)).toEqual("111th"); | ||
| }); | ||
|
||
|
|
||
| // Case 3: Large numbers | ||
| test("should handle larger numbers correctly", () => { | ||
| expect(getOrdinalNumber(101)).toEqual("101st"); | ||
| expect(getOrdinalNumber(112)).toEqual("112th"); | ||
| expect(getOrdinalNumber(123)).toEqual("123rd"); | ||
| }); | ||
|
|
||
| // Case 4: Invalid cases / inputs | ||
| test("should throw error for zero, negative or non-integer inputs", () => { | ||
| expect(() => getOrdinalNumber(0)).toThrow("Only positive integers are allowed"); | ||
| expect(() => getOrdinalNumber(-5)).toThrow("Only positive integers are allowed"); | ||
| expect(() => getOrdinalNumber(2.5)).toThrow("Only positive integers are allowed"); | ||
| }); | ||
|
|
||
| // Added possible and invalid cases and tested using npx jest | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,15 @@ | ||
| function repeat() { | ||
| return "hellohellohello"; | ||
| function repeat(str, count) { | ||
| if (typeof str !== "string") { | ||
| throw new Error("Input must be a string"); | ||
| } | ||
| if (!Number.isInteger(count) || count < 0) { | ||
| throw new Error("Count must be a non-negative integer"); | ||
| } | ||
|
|
||
| return str.repeat(count); | ||
| } | ||
|
|
||
| module.exports = repeat; | ||
|
|
||
|
|
||
| // Function implemented to repeat string count times. |
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.
Can you improve the indentation?
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.
Thank you, I have improved the indentation.