-
-
Notifications
You must be signed in to change notification settings - Fork 337
Cape Town | 26-ITP-Jan | Pretty Taruvinga | Sprint 3 | Practice TDD #1133
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
0b65a57
18ef756
9d908d9
715c649
2af6c11
4098eb1
77bc6be
b7a3a6f
92422ef
444a5ec
5ae7a46
d7f002f
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 count = 0; | ||
| for (let i = 0; i < stringOfCharacters.length; i++) { | ||
| if (stringOfCharacters[i] === findCharacter) { | ||
| count++; | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| module.exports = countChar; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| const suffixes = ["th", "st", "nd", "rd"]; | ||
| const v = num % 100; | ||
| return num + (suffixes[(v - 20) % 10] || suffixes[v] || suffixes[0]); | ||
|
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. Fancy! To check your understanding: Why not express line 4 as |
||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,31 @@ | ||
| const getOrdinalNumber = require("./get-ordinal-number"); | ||
| test("should append 'st' for numbers ending with 1, except those ending with 11", () => { | ||
| expect(getOrdinalNumber(1)).toEqual("1st"); | ||
| expect(getOrdinalNumber(21)).toEqual("21st"); | ||
| expect(getOrdinalNumber(131)).toEqual("131st"); | ||
| }); | ||
|
|
||
| // Case 2: Numbers ending with 2 (but not 12) | ||
| // When the number ends with 2, except those ending with 12, | ||
| // Then the function should return a string by appending "nd" to the number. | ||
| 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"); | ||
| }); | ||
|
|
||
| // In this week's prep, we started implementing getOrdinalNumber. | ||
| // This function takes a number as input and returns a string with the appropriate ordinal suffix ("st", "nd", "rd", "th"). | ||
| // For example: | ||
| // - getOrdinalNumber(1) should return "1st" | ||
|
|
||
| // Continue testing and implementing getOrdinalNumber for additional cases. | ||
| // Write your tests using Jest — remember to run your tests often for continual feedback. | ||
|
|
||
| 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"); | ||
| }); | ||
| // To ensure thorough testing, we need broad scenarios that cover all possible cases. | ||
| // Listing individual values, however, can quickly lead to an unmanageable number of test cases. | ||
| // Instead of writing tests for individual numbers, consider grouping all possible input values | ||
|
|
@@ -13,8 +35,18 @@ const getOrdinalNumber = require("./get-ordinal-number"); | |
| // Case 1: Numbers ending with 1 (but not 11) | ||
| // When the number ends with 1, except those ending with 11, | ||
| // Then the function should return a string by appending "st" to the number. | ||
| test("should append 'st' for numbers ending with 1, except those ending with 11", () => { | ||
| expect(getOrdinalNumber(1)).toEqual("1st"); | ||
| expect(getOrdinalNumber(21)).toEqual("21st"); | ||
| expect(getOrdinalNumber(131)).toEqual("131st"); | ||
| test("should append 'th' for numbers ending with 11, 12, or 13", () => { | ||
| expect(getOrdinalNumber(11)).toEqual("11th"); | ||
| expect(getOrdinalNumber(12)).toEqual("12th"); | ||
| expect(getOrdinalNumber(13)).toEqual("13th"); | ||
| }); | ||
|
|
||
| // Case 4: All other numbers | ||
| // When the number does not end with 1, 2, or 3 (or ends with 11, 12, or 13), | ||
| // Then the function should return a string by appending "th" to the number. | ||
| test("should append 'th' for all other numbers", () => { | ||
|
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. When a test fails with the message "... all other numbers", it may be unclear what "other numbers" actually refers to. |
||
| expect(getOrdinalNumber(4)).toEqual("4th"); | ||
| expect(getOrdinalNumber(10)).toEqual("10th"); | ||
| expect(getOrdinalNumber(14)).toEqual("14th"); | ||
| expect(getOrdinalNumber(100)).toEqual("100th"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,38 @@ | ||
| function repeatStr() { | ||
| return "hellohellohello"; | ||
| function repeatStr(n, str) { | ||
| let result = ""; | ||
| for (let i = 0; i < n; i++) { | ||
| result += str; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| module.exports = repeatStr; | ||
| // const repeatStr = require("./repeat-str"); | ||
| // Given a target string `str` and a positive integer `count`, | ||
| // When the repeatStr function is called with these inputs, | ||
| // Then it should: | ||
| // test("repeats a string n times", () => { | ||
| // expect(repeatStr(3, "hello")).toBe("hellohellohello"); | ||
| // }); | ||
| const repeatStr = require("./repeat-str"); | ||
| test("repeats a string n times", () => { | ||
| expect(repeatStr(3, "hello")).toBe("hellohellohello"); | ||
| }); | ||
| // test("repeats a different string", () => { | ||
| // expect(repeatStr(2, "abc")).toBe("abcabc"); | ||
| // }); | ||
| test("repeats a different string", () => { | ||
| expect(repeatStr(2, "abc")).toBe("abcabc"); | ||
| }); | ||
| // test("returns empty string when n is 0", () => { | ||
| // expect(repeatStr(0, "hi")).toBe(""); | ||
| // }); | ||
| test("returns empty string when n is 0", () => { | ||
| expect(repeatStr(0, "hi")).toBe(""); | ||
| }); | ||
| // test("returns empty string when n is 0", () => { | ||
| // expect(repeatStr(0, "hi")).toBe(""); | ||
| // }); | ||
| test("returns empty string when n is 0", () => { | ||
| expect(repeatStr(0, "hi")).toBe(""); | ||
| }); |
|
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. There is only one test in this script. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,96 @@ | ||
| // Implement a function repeatStr | ||
| const repeatStr = require("./repeat-str"); | ||
| // Given a target string `str` and a positive integer `count`, | ||
| // When the repeatStr function is called with these inputs, | ||
| // Then it should: | ||
|
|
||
| // Case: handle multiple repetitions: | ||
| // Given a target string `str` and a positive integer `count` greater than 1, | ||
| // When the repeatStr function is called with these inputs, | ||
| // Then it should return a string that contains the original `str` repeated `count` times. | ||
|
|
||
| test("should repeat the string count times", () => { | ||
| const str = "hello"; | ||
| const count = 3; | ||
| const repeatedStr = repeatStr(str, count); | ||
| const repeatedStr = repeatStr(count, str); | ||
| expect(repeatedStr).toEqual("hellohellohello"); | ||
| }); | ||
| // 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"); | ||
| // }); | ||
| // test("should return an empty string when count is 0", () => { | ||
| // const str = "hello"; | ||
| // const count = 0; | ||
| // const repeatedStr = repeatStr(str, count); | ||
| // expect(repeatedStr).toEqual(""); | ||
| // }); | ||
| // // Given a target string `str` and a positive integer `count`, | ||
| // // When the repeatStr function is called with these inputs, | ||
| // // Then it should: | ||
| // // - Validate that `count` is a non-negative integer. | ||
| // // - If `count` is 0, return an empty string. | ||
| // // - If `count` is 1, return the original `str`. | ||
| // // - If `count` is greater than 1, concatenate `str` to itself `count` times and return the resulting string. | ||
|
|
||
| // Case: handle count of 1: | ||
| // 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. | ||
|
|
||
| // 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. | ||
| // // Case: handle multiple repetitions: | ||
| // // Given a target string `str` and a positive integer `count` greater than 1, | ||
| // // When the repeatStr function is called with these inputs, | ||
| // // Then it should return a string that contains the original `str` repeated `count` times. | ||
| // test("should repeat the string count times", () => { | ||
| // const str = "hello"; | ||
| // const count = 3; | ||
| // const repeatedStr = repeatStr(str, count); | ||
| // expect(repeatedStr).toEqual("hellohellohello"); | ||
| // }); | ||
| // // Case: handle count of 1: | ||
| // // Given a target string `str` and a `count` equal to 1, | ||
| // // When the repeatStr function is called with these inputs, | ||
| // test("should repeat the string count times", () => { | ||
| // const str = "hello"; | ||
| // const count = 3; | ||
| // const repeatedStr = repeatStr(str, count); | ||
| // expect(repeatedStr).toEqual("hellohellohello"); | ||
| // }); | ||
|
|
||
| // 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. | ||
| // // Case: handle count of 1: | ||
| // // 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 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 = -1; | ||
| // expect(() => repeatStr(str, count)).toThrow("Count must be a non-negative integer"); | ||
| // }); | ||
| // // Case: Handle non-integer count: | ||
| // // Given a target string `str` and a non-integer value for `count`, | ||
| // // When the repeatStr function is called with these inputs, | ||
| // // Then it should throw an error, as non-integer counts are not valid. | ||
| // test("should throw an error when count is not an integer", () => { | ||
| // const str = "hello"; | ||
| // const count = 2.5; | ||
| // expect(() => repeatStr(str, count)).toThrow("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.
Nice touch to to show match is case sensitive.
Could also consider a case to show that the function is expected to work also for non-alphabets.