-
-
Notifications
You must be signed in to change notification settings - Fork 336
London|ITP-Jan-2026| Ping Wang | Sprint 3 | 2-Practice TDD #1009
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
Changes from all commits
984ec6c
7dac0c8
168ce77
81fc9ed
8acbbf2
649d804
9a2b257
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,13 @@ | ||
| 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("where","e")) | ||
|
|
||
| module.exports = countChar; |
|
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. Let's keep the file tidy without console logs unless it's the aim of the exercise. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,25 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| lastDigit = num % 10; | ||
| lastTwoDigits = num % 100; | ||
|
Comment on lines
+2
to
+3
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. While this, in theory, works, without a declaration keyword, they become implicit global variables, which could cause bugs. How can we fix that? |
||
|
|
||
| if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { | ||
| return num + "th"; | ||
| } | ||
|
|
||
| switch (lastDigit) { | ||
| case 1: | ||
| return num + "st"; | ||
| case 2: | ||
| return num + "nd"; | ||
| case 3: | ||
| return num + "rd"; | ||
| default: | ||
| return num + "th"; | ||
| } | ||
| } | ||
|
|
||
| console.log(getOrdinalNumber(112)) | ||
|
|
||
|
|
||
|
|
||
| module.exports = getOrdinalNumber; | ||
|
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. Let's keep the file tidy without console logs unless it's the aim of the exercise. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| function repeatStr() { | ||
| return "hellohellohello"; | ||
| function repeatStr(str,count) { | ||
| if (count>0) return str.repeat(count) | ||
| if (count===0) return "" | ||
|
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 would happen if we were to call str.repeat(0) ? Give it a shot, and once you do, how can we tweak this function slightly to save ourselves from one more if statement? |
||
| if (count<0) return"invalid count" | ||
|
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. The test is asking for an error to be thrown when the count is negative but we are instead returning a string, how can we address that? |
||
| } | ||
|
|
||
| module.exports = repeatStr; | ||
| console.log(repeatStr("hello",5)) | ||
| console.log(repeatStr("nihao",1)) | ||
| console.log(repeatStr("nihao",-5)) | ||
|
|
||
| module.exports = repeatStr; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,13 +20,22 @@ 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 repeat the string only once", () => { | ||
| expect(repeatStr("nihao",1)).toEqual("nihao"); | ||
| }); | ||
|
|
||
| // 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 repeat the string 0", () => { | ||
| expect(repeatStr("newyear",0)).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 repeat the string negative times", () => { | ||
| expect(repeatStr("celebration", -5)).toEqual("invalid count"); | ||
|
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. Throwing an error and returning a string is quite different, how can we fix this test? |
||
| }); | ||
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.
Let's keep the file tidy without console logs unless it's the aim of the exercise.