Repository navigation
London| 26-ITP-Sep | Alan Mak | Sprint 2 | practice-tdd #1714
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
fa80d75
bb0e6d7
7d54011
04b6e6f
0987e51
ba243f3
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,5 @@ | ||
| export function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| // Given a string `stringOfCharacters` and a single character `findCharacter` to search for, | ||
| const count = stringOfCharacters.split(findCharacter).length - 1; | ||
| return count | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| export function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| //Given a number `num`, the function should return a string representing the ordinal form of that number. | ||
| //When the number ends with 1, except those ending with 11, | ||
| //Then the function should return a string by appending "st" to the number. | ||
| if (num % 10 === 1 && num % 100 !== 11) return `${num}st`; | ||
|
|
||
| //When the number ends with 2, except those ending with 12, | ||
| //Then the function should return a string by appending "nd" to the number. | ||
| if (num % 10 === 2 && num % 100 !== 12) return `${num}nd`; | ||
|
|
||
| //When the number ends with 3, except those ending with 13, | ||
| //Then the function should return a string by appending "rd" to the number. | ||
| if (num % 10 === 3 && num % 100 !== 13) return `${num}rd`; | ||
|
|
||
| return `${num}th`; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,20 @@ | ||
| export function repeatStr() { | ||
| export function repeatStr(str, count) { | ||
| if (count < 0) { | ||
| throw new Error("Count must be a positive integer"); | ||
| } | ||
| if (count === 0) { | ||
| return ""; | ||
| } | ||
|
|
||
| // 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"; | ||
| let result = ""; | ||
| for (let i = 0; i < count; i++) { // i++ is i=i+1, i+=1, i=i+1, all are valid | ||
| result = result + str; //result += str; is also valid | ||
| } | ||
|
Comment on lines
+12
to
+14
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. Any of them will do. Suggestion: Use AI to explore "how to use comments in code?", and then clean up the comments. |
||
| return result; | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,13 +20,37 @@ 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 return the original string when count is 1", () => { | ||
| const str = "hello"; | ||
| const count = 1; | ||
| const repeatedStr = repeatStr(str, count); | ||
| expect(repeatedStr).toEqual("hello"); | ||
| }); | ||
|
|
||
| // 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 return an empty string when count is 0", () => { | ||
| const str = "hello"; | ||
| 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 when count is negative", () => { | ||
| const str = "hello"; | ||
| const count = -2; | ||
| expect(() => repeatStr(str, count)).toThrow(Error); | ||
| }); | ||
|
|
||
| test ("should repeat the string count times", () => { | ||
|
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 think of a better name for this test so it differs from others?
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. What about: test ("string should repeat itself for number of counts"); |
||
| const str = "abc"; | ||
| const count = 4; | ||
| const repeatedStr = repeatStr(str, count); | ||
| expect(repeatedStr).toEqual("abcabcabcabc"); | ||
| }); | ||
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.
Statement not consistently terminated by a semicolon. Why not use a formatter?
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.
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.
Prettier only support certain languages. For example, I know it does not support Python.
Here are two possible conditions I know from experience that Prettier may not work on JS code: