-
-
Notifications
You must be signed in to change notification settings - Fork 328
London | ITP-Jan-26 | Alexandru Pocovnicu | Sprint 3 | Practice TDD #987
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
57e4cd4
bfa31ef
7c85f26
3634d67
e78fcb2
d4b7e36
1c394fb
f870969
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,11 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| let sum = 0; | ||
| for (let char of stringOfCharacters) { | ||
| if (char === findCharacter) { | ||
| sum++; | ||
| } | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
| module.exports = countChar; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,15 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| let numberToString = String(num); | ||
| let numberLastDigit = numberToString[numberToString.length - 1]; | ||
| let numberLast2Digits = numberToString.slice(numberToString.length - 2); | ||
|
|
||
| if (numberLastDigit === "1" && numberLast2Digits !== "11") | ||
| return numberToString + "st"; | ||
| if (numberLastDigit === "2" && numberLast2Digits !== "12") | ||
| return numberToString + "nd"; | ||
| if (numberLastDigit === "3" && numberLast2Digits !== "13") | ||
| return numberToString + "rd"; | ||
| return numberToString + "th"; | ||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,31 @@ 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" to the number. | ||
| 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(122)).toEqual("122nd"); | ||
| }); | ||
|
|
||
| // 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" to the number. | ||
| test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { | ||
| expect(getOrdinalNumber(3)).toEqual("3rd"); | ||
| expect(getOrdinalNumber(23)).toEqual("23rd"); | ||
| expect(getOrdinalNumber(123)).toEqual("123rd"); | ||
| }); | ||
|
|
||
| // Case 4: Numbers ending with any number (but not 1,2 or 3) | ||
| // When the number ends with any number, except those ending with 1,2 or 3, | ||
| // Then the function should return a string by appending "th" to the number. | ||
| test("should append 'th' for numbers ending with any number including 11,12 or 13, except those ending with 1,2 or 3", () => { | ||
| expect(getOrdinalNumber(12)).toEqual("12th"); | ||
| expect(getOrdinalNumber(25)).toEqual("25th"); | ||
| expect(getOrdinalNumber(111)).toEqual("111th"); | ||
| expect(getOrdinalNumber(4)).toEqual("4th"); | ||
| }); | ||
|
Comment on lines
+43
to
+48
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. Can you add more test cases to this group to make the coverage more comprehensive? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| function repeatStr() { | ||
| return "hellohellohello"; | ||
| function repeatStr(str, count) { | ||
| if (count < 0) throw new Error("negative counts are not valid"); | ||
| return str.repeat(count); | ||
| } | ||
|
|
||
| module.exports = repeatStr; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,13 +20,28 @@ test("should repeat the string count times", () => { | |
| // Given a target string `str` and a `count` equal to 1, | ||
| // When the repeatStr function is called with these inputs, | ||
| // Then it should return the original `str` without repetition. | ||
| test("should repeat string count times", () => { | ||
| const str = "bye"; | ||
| const count = 1; | ||
| const repeatedStr = repeatStr(str, count); | ||
| expect(repeatedStr).toEqual("bye"); | ||
| }); | ||
|
|
||
| // Case: Handle count of 0: | ||
| // Given a target string `str` and a `count` equal to 0, | ||
| // When the repeatStr function is called with these inputs, | ||
| // Then it should return an empty string. | ||
| test("should repeat string count times", () => { | ||
|
Comment on lines
+23
to
+34
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. Can you revise these test descriptions to make the failure messages more informative, so it's immediately clear both what caused the test to fail and what the expected outcome was? |
||
| const str = "no"; | ||
| const count = 0; | ||
| const repeatedStr = repeatStr(str, count); | ||
| expect(repeatedStr).toEqual(""); | ||
| }); | ||
|
|
||
| // Case: Handle negative count: | ||
| // Given a target string `str` and a negative integer `count`, | ||
| // When the repeatStr function is called with these inputs, | ||
| // Then it should throw an error, as negative counts are not valid. | ||
| test("should throw an error for negative numbers", () => { | ||
| expect(() => repeatStr("str", -2)).toThrow("negative counts are not valid"); | ||
| }); | ||
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.
.slice(-N)is clear enough to indicate it extract the last N characters from a string, regardless of string length.