Repository navigation
London | 26-ITP-SEP | Russom Gebremeskel | Sprint 2 | Practice TDD #1723
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
301ea63
2501561
63e477b
a8e1b2d
9e2cef8
2ba2e34
06d5f91
3dc6da6
1e6cb55
61b2ce7
0407869
06d3f9a
598f744
e6fde3b
c2f743c
e7ff9c7
751fb78
4707f5a
f504687
d9197c4
9494c3d
582699c
b806b41
fcf27e7
04f544f
4815e87
9dc9e1f
10b0d0f
cccf002
ed2729e
4fa3e6c
ea5fa0f
6102a89
717584d
fed8ddd
bb815ef
c8fc9dd
3a565e9
c80ed87
38c28b3
f12c635
48b8dae
d5091de
fa07e40
71c6ea1
c12bed8
1b777bf
5d29d04
63905f0
be0c56e
f05ab1d
d6e7d06
2431fdf
7421372
51a7234
7465a37
b5ed6ae
d363d13
bd5de48
616bbbf
2de2d40
05f3e6e
ba2ebd0
2641052
ddd8985
e21c39a
96ea429
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,3 +1,9 @@ | ||
| export function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| let count = 0; | ||
| for (const char of stringOfCharacters) { | ||
| if (char === findCharacter) { | ||
| count++; | ||
| } | ||
| } | ||
| return count; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,15 @@ | ||
| export function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| const lastDigit = num % 10; | ||
| const lastTwoDigit = num % 100; | ||
|
|
||
| if (lastDigit === 1 && lastTwoDigit !== 11) { | ||
| return `${num}st`; | ||
| } | ||
| if (lastDigit === 2 && lastTwoDigit !== 12) { | ||
| return `${num}nd`; | ||
| } | ||
| if (lastDigit === 3 && lastTwoDigit !== 13) { | ||
| return `${num}rd`; | ||
| } | ||
| return `${num}th`; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,51 @@ 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(132)).toEqual("132nd"); | ||
| }); | ||
|
|
||
| // 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(133)).toEqual("133rd"); | ||
| }); | ||
|
|
||
| // Case 4: Exception for 11, 12 and 13. | ||
| // When a number ends with 11, 12 and 13, | ||
| // Then the function should return a string by appending "th" not st, nd or rd. | ||
| test("should append 'th' for numbers ending with 11, 12 and 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(1113)).toEqual("1113th"); | ||
| }); | ||
|
Comment on lines
+43
to
+50
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. These are numbers exactly equal to 11/12/13, how about larger numbers ended with 11/12/13 ? Please add those tests as well. Thank you.
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. Done. |
||
|
|
||
| // Case 5: Numbers ending with 4 to 9. | ||
| // When the number ends with 4 to 9, | ||
| // Then the function should return a string by appending "th" to the number. | ||
| test("should append 'th' for numbers ending with 4 to 9", () => { | ||
| expect(getOrdinalNumber(4)).toEqual("4th"); | ||
| expect(getOrdinalNumber(14)).toEqual("14th"); | ||
| expect(getOrdinalNumber(144)).toEqual("144th"); | ||
| }); | ||
|
|
||
| // Case 6: Numbers ending with 0. | ||
| // When the number ends with 0, | ||
| // Then the function should return a string by appending "th" to the number. | ||
| test("should append 'th' for numbers ending with 0", () => { | ||
| expect(getOrdinalNumber(0)).toEqual("0th"); | ||
| expect(getOrdinalNumber(10)).toEqual("10th"); | ||
| expect(getOrdinalNumber(100)).toEqual("100th"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,13 @@ | ||
| export function repeatStr() { | ||
| export function repeatStr(str, count) { | ||
| // Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat). | ||
| // The goal is to re-implement that function, not to use it. | ||
| return "hellohellohello"; | ||
| if (count < 0) { | ||
| throw new Error("Count must not be negative"); | ||
| } | ||
|
|
||
| let repeat = ""; | ||
| for (let i = 0; i < count; i++) { | ||
| repeat += str; | ||
| } | ||
| return repeat; | ||
| } |
|
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. Your test cases are correct. Just for you to think a little bit more, besides
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. An empty string? Additional test with empty string input is added. Thanks for your support with the review and feedback. 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. That's right. Also, it would be nice to test empty string with negative count to see whether it correctly throws error or incorrectly returns 0. Since your program logic is correct, I will let you pass for this one and just have a better test cases coverage in the future. Thank you. |
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.
This test case is correct. However, there should be a special
strwhich gives 0 anyway. Can you think of it? If so, please add it as well. Thank you.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.
Would that be an empty string("") ?
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.
Good try and almost correct. However, your original test case of counting
char = "b"instr = "aaaaa"should still be kept. For the empty string test, it should be countingchar = "b"in empty stringstr = "", but not counting empty characterchar = ""instr = "aaaaa"as one could say there are 6 (or even infinity) of empty occurrences between the 5as. Sochar = ""is not required in this exercise butstr = ""would be needed.