-
-
Notifications
You must be signed in to change notification settings - Fork 272
London | 25-ITP-Sep | Payman Issa Baiglu | sprint 3 | 2-practice-tdd #883
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 3 commits
92afa66
9e894a7
ded405e
a124398
e7d7fff
fbacf4a
3585be7
bbc996c
5ee3f6b
354b5ac
fdc1b1a
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,12 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| } | ||
| let count = 0; | ||
| for (let i = 0; i < stringOfCharacters.length; i++) { | ||
| if (stringOfCharacters[i] === findCharacter) { | ||
| count++; | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
| console.log(countChar("hello world!", "l")); | ||
|
|
||
| module.exports = countChar; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,13 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| const suffixes = ["th", "st", "nd", "rd"]; | ||
| const v = num % 100; | ||
| console.log(v); | ||
| if (v >= 11 && v <= 13) { | ||
| return num + "th"; | ||
| } | ||
| const suffix = suffixes[(v % 10)] || "th"; | ||
| return num + suffix; | ||
| } | ||
| console.log(getOrdinalNumber(52)); | ||
|
|
||
| module.exports = getOrdinalNumber; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,17 @@ const getOrdinalNumber = require("./get-ordinal-number"); | |
| test("should return '1st' for 1", () => { | ||
| expect(getOrdinalNumber(1)).toEqual("1st"); | ||
| }); | ||
|
|
||
| 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 '24th' for 24", () => { | ||
| expect(getOrdinalNumber(24)).toEqual("24th"); | ||
| }); | ||
| test("should return '11th' for 11", () => { | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| }); | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,16 @@ | ||
| function repeat() { | ||
| return "hellohellohello"; | ||
| function repeat(str , count) { | ||
| if (count < 0) { | ||
| return "Count must be a non-negative integer"; | ||
| } | ||
|
Comment on lines
2
to
4
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. How would the caller distinguish the result of the following two function calls?
Both function calls return the same value.
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. Thanks, I see your point. I changed it so it throw error. |
||
| if (count === 0) { | ||
| return ""; | ||
| } | ||
| for (let i = 0; i < count; i++) { | ||
| return str.repeat(count); | ||
| } | ||
|
||
| } | ||
| console.log(repeat("hello", -2)); | ||
|
|
||
|
|
||
|
|
||
| module.exports = repeat; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,5 @@ | ||
| // Implement a function repeat | ||
| const repeat = require("./repeat"); | ||
| // Given a target string str and a positive integer count, | ||
| // When the repeat function is called with these inputs, | ||
| // Then it should: | ||
|
|
||
| // case: repeat String: | ||
| // Given a target string str and a positive integer count, | ||
| // When the repeat function is called with these inputs, | ||
|
|
@@ -20,13 +16,29 @@ test("should repeat the string count times", () => { | |
| // Given a target string str and a count equal to 1, | ||
| // 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 return the original string when count is 1", () => { | ||
| const str = "world"; | ||
| const count = 1; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("world"); | ||
| }); | ||
| // 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 when count is 0", () => { | ||
| const str = "test"; | ||
| const count = 0; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual(""); | ||
| }); | ||
| // 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 a message for negative count", () => { | ||
| const str = "error"; | ||
| const count = -2; | ||
| const repeatedStr = repeat(str, count); | ||
| expect(repeatedStr).toEqual("Count must be a non-negative integer"); | ||
| }); | ||
|
||
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.
v?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.
Thanks @cjyuan. I updated the V variable to date to make it more meaningful. also removed the console.logs to make it more clean.