Repository navigation
London | 26-ITP-Sep | Hanna Bohlin | Sprint 2 | Practice TDD #1720
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
38dbbd8
6b5df27
ffcf1f9
9f80255
b7fcec6
626f314
b184661
e59481e
ce8ef88
3da27c9
ece5aaf
41c6f58
c4ed549
dbb63f0
d8bc6e0
43522a0
54e7f2e
5739486
6721f24
582352f
ed6c46c
8ad33fc
b21915a
4ef1b43
8d41b97
719b29d
c4fa2c6
5c15ff4
7b78cdd
eae160f
c032f5b
8614766
8c54303
74f5c33
23e9048
803ce36
5a8d70f
ceaec78
63998f9
0aa534c
1724b9a
330bd08
9574101
c939e57
9633c24
441d1bf
4abb6da
dee4647
57792ce
f263f07
df75194
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,11 @@ | ||
| export function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| let numOfChar = 0; | ||
|
|
||
| for (let i = 0; i < stringOfCharacters.length; i++) { | ||
| if (stringOfCharacters.charAt(i) === findCharacter) { | ||
| numOfChar++; | ||
| } | ||
| } | ||
|
|
||
| return numOfChar; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,33 @@ | ||
| export function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| if ( | ||
| num <= 0 || | ||
| num % 1 !== 0 || | ||
| !num || | ||
| typeof num !== "number" || | ||
| num.length > 1 | ||
|
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. This line is no longer needed either. The only values that get this far are numbers, because line 6 has already caught everything else, and a number has no |
||
| ) { | ||
| throw new Error("Error: positive whole number is needed"); | ||
| } | ||
|
|
||
| if ( | ||
| (num - 11) % 100 === 0 || | ||
| (num - 12) % 100 === 0 || | ||
| (num - 13) % 100 === 0 | ||
| ) { | ||
| return num + "th"; | ||
| } | ||
|
|
||
| if ((num - 1) % 10 === 0) { | ||
| return num + "st"; | ||
| } | ||
|
|
||
| if ((num - 2) % 10 === 0) { | ||
| return num + "nd"; | ||
| } | ||
|
|
||
| if ((num - 3) % 10 === 0) { | ||
| return num + "rd"; | ||
| } | ||
|
|
||
| return num + "th"; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,74 @@ 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 11. Should append "th" to the number | ||
| test("should append 'th' for numbers ending with 11", () => { | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| expect(getOrdinalNumber(111)).toEqual("111th"); | ||
| expect(getOrdinalNumber(4211)).toEqual("4211th"); | ||
| }); | ||
|
Comment on lines
+21
to
+27
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. Great separation of behaviours for this test suite! 👌 |
||
|
|
||
| // Case 3: Numbers ending with 2 (but not 12). Should append "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 4: Numbers ending with 12. Should append "th" to the number | ||
| test("should append 'th' for numbers ending with 12", () => { | ||
| expect(getOrdinalNumber(12)).toEqual("12th"); | ||
| expect(getOrdinalNumber(112)).toEqual("112th"); | ||
| expect(getOrdinalNumber(4212)).toEqual("4212th"); | ||
| }); | ||
|
|
||
| // Case 5: Numbers ending with 3 (but not 13). Should append "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 6: Numbers ending with 13. Should append "th" to the number | ||
| test("should append 'th' for numbers ending with 13", () => { | ||
| expect(getOrdinalNumber(13)).toEqual("13th"); | ||
| expect(getOrdinalNumber(113)).toEqual("113th"); | ||
| expect(getOrdinalNumber(4213)).toEqual("4213th"); | ||
| }); | ||
|
|
||
| // Case 7: All other positive whole numbers. Should append "th" to the number | ||
| test("should append 'th' for all other positive whole numbers", () => { | ||
| expect(getOrdinalNumber(1834)).toEqual("1834th"); | ||
| expect(getOrdinalNumber(23151509)).toEqual("23151509th"); | ||
| expect(getOrdinalNumber(100)).toEqual("100th"); | ||
| }); | ||
|
|
||
| // Case 8: Throw error for invalid-numbers | ||
| test("should throw error for all invalid numbers", () => { | ||
| expect(() => { | ||
| getOrdinalNumber(5.7); | ||
| }).toThrow("Error: positive whole number is needed"); | ||
| expect(() => { | ||
| getOrdinalNumber(-3859); | ||
| }).toThrow("Error: positive whole number is needed"); | ||
| expect(() => { | ||
| getOrdinalNumber(0); | ||
| }).toThrow("Error: positive whole number is needed"); | ||
| }); | ||
|
|
||
| // Case 9: Throw error for non-numbers | ||
| test("should throw error for all non-numbers", () => { | ||
| expect(() => { | ||
| getOrdinalNumber(); | ||
| }).toThrow("Error: positive whole number is needed"); | ||
| expect(() => { | ||
| getOrdinalNumber("hey"); | ||
| }).toThrow("Error: positive whole number is needed"); | ||
| expect(() => { | ||
| getOrdinalNumber(undefined); | ||
| }).toThrow("Error: positive whole number is needed"); | ||
| expect(() => { | ||
| getOrdinalNumber(true); | ||
| }).toThrow("Error: positive whole number is needed"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,13 @@ | ||
| export function repeatStr() { | ||
| // 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"; | ||
| export function repeatStr(str, count) { | ||
| if (count < 0) { | ||
| throw new Error("/Error: count should be non negative integer/"); | ||
| } | ||
|
|
||
| let repeatedStr = ""; | ||
|
|
||
| for (let i = 0; i < count; i++) { | ||
| repeatedStr += str; | ||
| } | ||
|
|
||
| return repeatedStr; | ||
| } |
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 line is no longer needed.
!numis only true for0,NaN,undefined,null,""andfalse, and every one of those is already caught bynum <= 0ornum % 1 !== 0on lines 3 and 4. Because||stops at the firsttrue, this condition never decides anything.