-
-
Notifications
You must be signed in to change notification settings - Fork 268
London | 25-ITP-September | Alexandru Pocovnicu | Coursework/sprint 3 | 2 practice tdd #776
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
7b93eed
86a4f93
7429d54
a59de40
9af1dec
de7beb8
dd1f125
a2ae864
cf3d5cd
7f9d151
253d984
0f7f656
ade9346
a1e9f60
5d5d095
6c0be54
1a64458
eee22d9
a4b2b74
90a7852
7198839
b91f950
9c86871
e28e199
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,18 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| let char = 0; | ||
| if (stringOfCharacters === 0){ | ||
| return 0 | ||
| } | ||
| for(let i = 0;i <= stringOfCharacters.length - findCharacter.length;i++){ | ||
| if ( findCharacter.length === 0 || stringOfCharacters === 0){ | ||
| return 0; | ||
| } | ||
| if(stringOfCharacters.substring(i, i + findCharacter.length ) === findCharacter){ | ||
| char++; | ||
| } | ||
| } | ||
| return char | ||
| } | ||
|
|
||
| module.exports = countChar; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,30 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| if (typeof num !== "number") { | ||
| return NaN; | ||
| } | ||
| if (!Number.isInteger(num)) { | ||
| return "not an integer number"; | ||
| } | ||
| if (num === 0) { | ||
| return "invalid number"; | ||
| } | ||
|
|
||
| const lastDigit = Number(num.toString().slice(-1)); | ||
| const lastTwoDigits = Number(num.toString().slice(-2)); | ||
|
|
||
| if (lastTwoDigits >= 10 && lastTwoDigits <= 13) { | ||
| return `${num}th`; | ||
| } | ||
|
|
||
| if (lastDigit === 1) { | ||
| return `${num}st`; | ||
| } else if (lastDigit === 2) { | ||
| return `${num}nd`; | ||
| } else if (lastDigit === 3) { | ||
| return `${num}rd`; | ||
| } | ||
|
|
||
| return `${num}th`; | ||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,63 @@ const getOrdinalNumber = require("./get-ordinal-number"); | |
| // When the number is 1, | ||
| // Then the function should return "1st" | ||
|
|
||
| test("should return 'not an integer number' for 1.5", () => { | ||
| expect(getOrdinalNumber(1.5)).toEqual("not an integer number"); | ||
| }); | ||
|
|
||
| test("should return 'invalid number' for 0", () => { | ||
| expect(getOrdinalNumber(0)).toEqual("invalid number"); | ||
| }); | ||
|
|
||
| test("should return '1st' for 1", () => { | ||
| expect(getOrdinalNumber(1)).toEqual("1st"); | ||
| }); | ||
|
|
||
| test("should return '121st' for 121", () =>{ | ||
|
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. These test cases are good but I think there are more edge cases that you could cover to make sure your function behaves in a way you designed. For example, what happens when you pass 0 or 1.5? Also, I think you could have more comprehensive test cases for numbers. With the current bug you have
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. not sure about the negative numbers, left the test in there anyway |
||
| expect(getOrdinalNumber(121)).toEqual("121st") | ||
| }) | ||
|
|
||
| test("should return '2nd' for 2", () => { | ||
| expect(getOrdinalNumber(2)).toEqual("2nd"); | ||
| }); | ||
|
|
||
|
|
||
| test("should return '3rd' for 3", () => { | ||
| expect(getOrdinalNumber(3)).toEqual("3rd"); | ||
| }); | ||
|
|
||
| test("should return '33rd' for 33", () => { | ||
| expect(getOrdinalNumber(33)).toEqual("33rd"); | ||
| }); | ||
|
|
||
| test("should return '5th' for 5", () => { | ||
| expect(getOrdinalNumber(5)).toEqual("5th"); | ||
| }); | ||
|
|
||
| test("should return '1114th' for 1114", () => { | ||
| expect(getOrdinalNumber(1114)).toEqual("1114th"); | ||
| }); | ||
|
|
||
| test("should return '11th' for 11", () => { | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| }); | ||
|
|
||
| test("should return '-11th' for -11", () => { | ||
| expect(getOrdinalNumber(-11)).toEqual("-11th"); | ||
| }); | ||
|
|
||
| test("should return '112th' for 112", () => { | ||
| expect(getOrdinalNumber(112)).toEqual("112th"); | ||
| }); | ||
|
|
||
| test("should return '11113th' for 11113", () => { | ||
| expect(getOrdinalNumber(11113)).toEqual("11113th"); | ||
| }); | ||
|
|
||
| test("should return NaN for s", () => { | ||
| expect(getOrdinalNumber("s")).toEqual(NaN); | ||
| }); | ||
|
|
||
| test("should return NaN for '9'", () => { | ||
| expect(getOrdinalNumber("9")).toEqual(NaN); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| function repeat() { | ||
| return "hellohellohello"; | ||
| function repeat(str, count) { | ||
| if (typeof str !== "string") { | ||
| return "Error: str must be a string"; | ||
| } | ||
| if (count < 0 || count === undefined || typeof count !== "number") { | ||
| return "Error : count must be a positive integer"; | ||
| } | ||
| if (count === 0) { | ||
| return ""; | ||
| } | ||
| return str.repeat(count); | ||
| } | ||
|
|
||
| module.exports = repeat; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,12 +21,117 @@ test("should repeat the string count times", () => { | |
| // When the repeat function is called with these inputs, | ||
| // Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. | ||
|
|
||
| test("should not repeat the string", ()=>{ | ||
|
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. Same as above, these test cases are good but they are a bit slim. You could add couple of more tests to test different variations of count and try to break your function by adding some tests for edge cases, like in the case you pass str="".
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 added a few more tests for str as for count the requirement says it's a positive integer , no point to test for NaN etc, I also commented out the test for count = 0 , 0 not being a positive integer.
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. I think 0 is a nice corner case you should have in your test case. Regarding the requirement, yes, there is a test for positive values, but do you see why we actually need that test case? It's not just because requirements say count must be positive. It's because JavaScript's
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. You can think of it another way: you can't repeat something negative times, and that's why you have that unit test. Following the same logic, you also can't repeat something undefined times, right? So whether you're thinking about the function logically or from a technical perspective, there's always a set of inputs it shouldn't work with. Writing test cases for these different scenarios helps you catch those edge cases. |
||
| const str = "hello"; | ||
| const count = 1; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("hello") | ||
| }) | ||
|
|
||
| // case: Handle Count of 0: | ||
| // Given a target string str and a count equal to 0, | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should return an empty string, ensuring that a count of 0 results in an empty output. | ||
|
|
||
| test("should return an empty string", ()=>{ | ||
| const str = "hello"; | ||
| const count = 0; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual(""); | ||
| }) | ||
|
|
||
| // case: Handle str not a string: | ||
| // Given a number and a count equal to 2, | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should return an error message. | ||
|
|
||
| test("should return an error message", ()=>{ | ||
| const str = 5; | ||
| const count = 5; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("Error: str must be a string"); | ||
| }) | ||
|
|
||
| // case: Handle Count of Undefined: | ||
| // Given a target string str and a count equal to Undefined, | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should return an error message. | ||
|
|
||
| test("should return an error message", ()=>{ | ||
| const str = "hello"; | ||
| const count = undefined; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("Error : count must be a positive integer"); | ||
| }) | ||
|
|
||
| // case: Handle Count of array: | ||
| // Given a target string str and a count equal to [], | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should return an error message. | ||
|
|
||
| test("should return an error message", ()=>{ | ||
| const str = "hello"; | ||
| const count = []; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("Error : count must be a positive integer"); | ||
| }) | ||
|
|
||
| // case: Negative Count: | ||
| // Given a target string str and a negative integer count, | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should throw an error or return an appropriate error message, as negative counts are not valid. | ||
|
|
||
| test("should return an error message", ()=>{ | ||
| const str="hello"; | ||
| const count=-1; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("Error : count must be a positive integer"); | ||
| }) | ||
|
|
||
| // case: Handle empty string: | ||
| // Given a target string str and a count equal to 2, | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should return an empty string, ensuring that a count of 2 results in an empty output. | ||
|
|
||
| test("should return an empty string", () => { | ||
| const str = ""; | ||
| const count = 2; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual(""); | ||
| }); | ||
|
|
||
| // case: Handle string with space: | ||
| // Given a target string str and a count equal to 2, | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should repeat the str count times and return a new string containing the repeated str values. | ||
|
|
||
| test("should repeat the string count times", () => { | ||
| const str = "a b"; | ||
| const count = 2; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("a ba b"); | ||
| }); | ||
|
|
||
| // case: Handle long string : | ||
| // Given a target string str and a count equal to 1, | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should repeat the str count times and return a new string containing the repeated str values. | ||
|
|
||
| test("should not repeat the string", () => { | ||
| const str = "xvg56756yrhfghe5ujdfh45657tjrtg6yrthrty"; | ||
| const count = 1; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("xvg56756yrhfghe5ujdfh45657tjrtg6yrthrty"); | ||
| }); | ||
|
|
||
| // case: Handle single character string and high count : | ||
| // Given a target string str and a count equal to 18, | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should repeat the str count times and return a new string containing the repeated str values. | ||
|
|
||
| test("should repeat str 18 times", () => { | ||
| const str = "x"; | ||
| const count = 18; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("xxxxxxxxxxxxxxxxxx"); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.