-
-
Notifications
You must be signed in to change notification settings - Fork 391
Manchester | 26-ITP-May | Yee Man Tsang | Sprint 3 | 2-practice-tdd #1507
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
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,11 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| let countTime = 0; | ||
| for (let i = 0; i < stringOfCharacters.length; i++) { | ||
| if (stringOfCharacters.slice(i, i + 1) == findCharacter) {//take each character out from string to compare with the given character. If true, then add one to countTime. | ||
| countTime = countTime + 1; | ||
| } | ||
| } | ||
| return countTime; | ||
| } | ||
|
|
||
| module.exports = countChar; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,36 @@ test("should count multiple occurrences of a character", () => { | |
| expect(count).toEqual(5); | ||
| }); | ||
|
|
||
| test("should count multiple occurrences of a character", () => { | ||
| const str = "aaabcde"; | ||
| const char = "c"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(1); | ||
| }); | ||
|
|
||
| test("should count multiple occurrences of a character", () => { | ||
| const str = "a2b3c4"; | ||
| const char = "3"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(1); | ||
| }); | ||
|
|
||
| test("should count multiple occurrences of a character", () => { | ||
| const str = "4444444444444444444"; | ||
| const char = "4"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(19); | ||
| }); | ||
|
|
||
|
Comment on lines
+20
to
+40
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. Tests should have different descriptions - here we have 3 tests, all with the same description. There's two possible options here:
Option 1 should be used when you have multiple assertions testing the same kind of behaviour. Option 2 should be used when you are testing different behaviours but have accidentally given them the same description |
||
| // Scenario: No Occurrences | ||
| // Given the input string `str`, | ||
| // And a character `char` that does not exist within `str`. | ||
| // When the function is called with these inputs, | ||
| // Then it should return 0, indicating that no occurrences of `char` were found. | ||
|
|
||
| test("should count no occurrence of a character", () => { | ||
| const str = "fghij"; | ||
| const char = "b"; | ||
| const count = countChar(str, char); | ||
| expect(count).toEqual(0); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,43 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| let onesDigit; | ||
|
|
||
| if (String(num).length > 1) { | ||
| //check if the number has more than 1 digit | ||
| onesDigit = Number(String(num).slice(-1)); //extract the figure at the tens digit, and convert it into a number. | ||
| const tensDigit = Number(String(num).slice(-2, -1)); //extract the figure at the tens digit, and convert it into a number. | ||
|
|
||
| if (onesDigit == 1) { | ||
| if (tensDigit == 1) { | ||
| return num + "th"; | ||
| } else { | ||
| return num + "st"; | ||
| } | ||
| } else if (onesDigit == 2) { | ||
| if (tensDigit == 1) { | ||
| return num + "th"; | ||
| } | ||
| return num + "nd"; | ||
| } else if (onesDigit == 3) { | ||
| if (tensDigit == 1) { | ||
| return num + "th"; | ||
| } | ||
| return num + "rd"; | ||
| } else { | ||
| ("th"); | ||
| } | ||
| } else if (String(num).length == 1) { | ||
| onesDigit = Number(String(num)[0]); //extract the figure at the tens digit, and convert it into a number. | ||
| if (onesDigit == 1) { | ||
| return num + "st"; | ||
| console.log("Yeah youre in"); | ||
| } else if (onesDigit == 2) { | ||
| return num + "nd"; | ||
| } else if (onesDigit == 3) { | ||
| return num + "rd"; | ||
| } else { | ||
| return num + "th"; | ||
| } | ||
| } | ||
|
Comment on lines
+4
to
+40
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 is a mostly working solution, but with a lot of nested if statements it can be quite hard to follow the logic of it. A few things to consider:
|
||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,25 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" | |
| expect(getOrdinalNumber(21)).toEqual("21st"); | ||
| expect(getOrdinalNumber(131)).toEqual("131st"); | ||
| }); | ||
|
|
||
| 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"); | ||
| }); | ||
|
|
||
| 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"); | ||
| }); | ||
|
|
||
| test("should append 'th' for other numbers without ending with 1,2 or 3, except those ending with 1 in tens digit", () => { | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| expect(getOrdinalNumber(12)).toEqual("12th"); | ||
| expect(getOrdinalNumber(13)).toEqual("13th"); | ||
| expect(getOrdinalNumber(113)).toEqual("113th"); | ||
| expect(getOrdinalNumber(213)).toEqual("213th"); | ||
| expect(getOrdinalNumber(313)).toEqual("313th"); | ||
| expect(getOrdinalNumber(1113)).toEqual("1113th"); | ||
| }); | ||
|
Comment on lines
+21
to
+42
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 tests are pretty good! Though every assertion here ends in 1, 2, or 3. If you added an assertion with a number ending in a 4, what would happen? Would it pass? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,17 @@ | ||
| function repeatStr() { | ||
| function repeatStr(str, count) { | ||
| // 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 strOutput = ""; | ||
| if (count > 0){ | ||
| for (let i=0 ; i<count ; i++){ | ||
| strOutput = strOutput + str; | ||
| } | ||
| return strOutput; | ||
| } else if (count == 0){ | ||
| return ""; | ||
|
Comment on lines
+4
to
+11
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 |
||
| }else{ | ||
| return "error"; | ||
| } | ||
| } | ||
|
|
||
| module.exports = repeatStr; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,13 +20,31 @@ 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 string with no reptition", () => { | ||
| const str = "morning"; | ||
| const count = 1; | ||
| const repeatedStr = repeatStr(str, count); | ||
| expect(repeatedStr).toEqual("morning"); | ||
| }); | ||
|
|
||
| // 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 empty string as the count is zero", () => { | ||
| const str = "afternoon"; | ||
| 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 return the string with no reptition", () => { | ||
| const str = "evening"; | ||
| const count = -1; | ||
| const repeatedStr = repeatStr(str, count); | ||
| expect(repeatedStr).toEqual("error"); | ||
| }); | ||
|
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.
|
||
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 definitely works! But
is quite a lengthy way to access a single character in a string. Can you think of a simpler way?