-
-
Notifications
You must be signed in to change notification settings - Fork 274
London | 25-ITP-Sep | KME Blair | Sprint 3 | Implement and Rewrite Tests Coursework #861
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
781ac9e
419c9ba
63dc46f
6d76dee
1c30f4c
05f0fb6
e7b918f
c07c2ef
9970733
ce4ebd7
484da5d
4bf763d
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 |
|---|---|---|
|
|
@@ -8,9 +8,17 @@ | |
| // write one test at a time, and make it pass, build your solution up methodically | ||
| // just make one change at a time -- don't rush -- programmers are deep and careful thinkers | ||
| function getCardValue(card) { | ||
| let rank = card.charAt(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. Does your function return the value you expected from each of the following function calls? |
||
| if (rank === "A") { | ||
| return 11; | ||
| } else if (rank >= "2" && rank <= "9") { | ||
| return parseInt(rank); | ||
| } else if (rank === "K" || rank === "Q" || rank === "J") { | ||
| return 10; | ||
| } else if (rank === "1") { | ||
| return 10; | ||
| } | ||
| return "Invalid card rank."; | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -39,19 +47,26 @@ assertEquals(aceofSpades, 11); | |
| // When the function is called with such a card, | ||
| // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). | ||
| const fiveofHearts = getCardValue("5♥"); | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
| assertEquals(fiveofHearts, 5); | ||
|
|
||
| // Handle Face Cards (J, Q, K): | ||
| // Given a card with a rank of "10," "J," "Q," or "K", | ||
| // When the function is called with such a card, | ||
| // Then it should return the value 10, as these cards are worth 10 points each in blackjack. | ||
| const faceCards = getCardValue("10" || "J" || "Q" || "K"); | ||
| assertEquals(faceCards, 10); | ||
|
|
||
| // Handle Ace (A): | ||
| // Given a card with a rank of "A", | ||
| // When the function is called with an Ace, | ||
| // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. | ||
| const anyAce = getCardValue("A"); | ||
| assertEquals(anyAce, 11); | ||
|
|
||
| // Handle Invalid Cards: | ||
| // Given a card with an invalid rank (neither a number nor a recognized face card), | ||
| // When the function is called with such a card, | ||
| // Then it should throw an error indicating "Invalid card rank." | ||
|
|
||
| const invalidCard = getCardValue("z"); | ||
| assertEquals(invalidCard, "Invalid card rank."); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,14 @@ test("should return true for a proper fraction", () => { | |
| expect(isProperFraction(2, 3)).toEqual(true); | ||
| }); | ||
|
|
||
| // Case 2: Identify Improper Fractions: | ||
| test("should return false for an improper fraction", () => { | ||
| expect(isProperFraction(5, 2)).toEqual(false); | ||
|
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. We can write multiple |
||
| }); | ||
|
|
||
| // Case 3: Identify Negative Fractions: | ||
| test("should return true for an negative fraction", () => { | ||
| expect(isProperFraction(-4, 7)).toEqual(true); | ||
| }); | ||
|
Comment on lines
+13
to
+15
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. The test description is misleading because not all negative fractions are proper fraction. For example, |
||
|
|
||
| // Case 4: Identify Equal Numerator and Denominator: | ||
| test("should return false when the numerator equals the denominator", () => { | ||
| expect(isProperFraction(3, 3)).toEqual(false); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,24 @@ test("should return 11 for Ace of Spades", () => { | |
| expect(aceofSpades).toEqual(11); | ||
| }); | ||
|
|
||
| // Case 2: Handle Number Cards (2-10): | ||
| // Case 3: Handle Face Cards (J, Q, K): | ||
| test("should return 5 for 5 of Hearts", () => { | ||
| const fiveofHearts = getCardValue("5♥"); | ||
| expect(fiveofHearts).toEqual(5); | ||
| }); | ||
|
Comment on lines
+10
to
+13
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.
For example, one possible category for |
||
|
|
||
| test("should return 10 for Face Cards", () => { | ||
| const faceCards = getCardValue("10" || "J" || "Q" || "K"); | ||
|
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.
Note: 10 is considered a number card, not a face card. |
||
| expect(faceCards).toEqual(10); | ||
| }); | ||
|
|
||
| // Case 4: Handle Ace (A): | ||
| test("should return 11 for Aces", () => { | ||
| const anyAce = getCardValue("A"); | ||
| expect(anyAce).toEqual(11); | ||
| }); | ||
|
|
||
|
Comment on lines
20
to
+25
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. Ace is handled on lines 5-8. We don't really need another test for Ace. |
||
| // Case 5: Handle Invalid Cards: | ||
| test("should return 'Invalid card rank.' for invalid cards", () => { | ||
| const invalidCard = getCardValue("z"); | ||
| expect(invalidCard).toEqual("Invalid card rank."); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| return stringOfCharacters.split(findCharacter).length - 1; | ||
| } | ||
|
|
||
| module.exports = countChar; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,23 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| if (num % 100 >= 11 && num % 100 <= 13) { | ||
| return `${num}th`; | ||
| } | ||
| const lastDigit = num % 10; | ||
| let suffix = "th"; | ||
|
|
||
| switch (lastDigit) { | ||
| case 1: | ||
| suffix = "st"; | ||
| break; | ||
| case 2: | ||
| suffix = "nd"; | ||
| break; | ||
| case 3: | ||
| suffix = "rd"; | ||
| break; | ||
| } | ||
|
|
||
| return `${num}${suffix}`; | ||
| } | ||
|
|
||
| module.exports = getOrdinalNumber; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,10 +4,30 @@ const getOrdinalNumber = require("./get-ordinal-number"); | |
| // continue testing and implementing getOrdinalNumber for additional cases | ||
| // Write your tests using Jest - remember to run your tests often for continual feedback | ||
|
|
||
| // Case 1: Identify the ordinal number for 1 | ||
| // When the number is 1, | ||
| // Then the function should return "1st" | ||
|
|
||
| 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 "13th" for 13', () => { | ||
| expect(getOrdinalNumber(13)).toEqual("13th"); | ||
| }); | ||
|
|
||
| test("should return '41st' for 41", () => { | ||
| expect(getOrdinalNumber(41)).toEqual("41st"); | ||
| }); | ||
|
|
||
| test('should return "212th" for 212', () => { | ||
| expect(getOrdinalNumber(212)).toEqual("212th"); | ||
| }); | ||
|
|
||
| test("should return '221st' for 221", () => { | ||
| expect(getOrdinalNumber(221)).toEqual("221st"); | ||
| }); | ||
|
Comment on lines
7
to
+33
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. To ensure thorough testing, we need broad scenarios that cover all possible cases. For example, we can prepare a test for numbers 2, 22, 132, etc. as |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| function repeatStr() { | ||
| return "hellohellohello"; | ||
| function repeatStr(str, count) { | ||
| if (count < 0) { | ||
| return "Error, 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. |
||
|
|
||
| return str.repeat(count); | ||
| } | ||
|
|
||
| module.exports = repeatStr; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,17 +16,23 @@ test("should repeat the string count times", () => { | |
| 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, | ||
| // 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 without repetition", () => { | ||
| 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, ensuring that a count of 0 results in an empty output. | ||
| test("should return an empty string if count is 0", () => { | ||
| const str = "hello"; | ||
| const count = 0; | ||
| const repeatedStr = repeatStr(str, count); | ||
| expect(repeatedStr).toEqual(""); | ||
| }); | ||
|
|
||
| // case: 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 or return an appropriate error message, as negative counts are not valid. | ||
| test("should throw an error if count is negative", () => { | ||
| const str = "hello"; | ||
| const count = -1; | ||
| const repeatedStr = repeatStr(str, count); | ||
| expect(repeatedStr).toEqual("Error, negative integer"); | ||
| }); | ||
|
Comment on lines
+33
to
+38
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. If you modified |
||
|
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. Are the files in |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Document</title> | ||
| <script defer src="script.js"></script> | ||
| </head> | ||
| <body> | ||
| <section> | ||
| <h3>Character limit</h3> | ||
| <label for="comment-input" | ||
| >Please enter your comment in the text area below | ||
| </label> | ||
| <textarea | ||
| id="comment-input" | ||
| name="comment-input" | ||
| rows="5" | ||
| maxlength="200" | ||
| ></textarea> | ||
| <p id="character-limit-info"></p> | ||
| </section> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| const textarea = document.querySelector("textarea"); | ||
| const remainingCharacters = textarea.maxLength - textarea.value.length; | ||
| console.log(textarea.maxLength); | ||
|
|
||
| const charactersLeftP = document.querySelector("#character-limit-info"); | ||
| charactersLeftP.innerText = `You have ${remainingCharacters} characters remaining`; | ||
|
|
||
|
|
||
|
|
||
| textarea.addEventListener("keyup", | ||
| () => const remainingCharacters = textarea.maxLength - textarea.value.length; | ||
| const charactersLeftP = document.querySelector("#character-limit-info"); | ||
| charactersLeftP.innerText = `You have ${remainingCharacters} characters remaining`; | ||
| console.log(remainingCharacters); | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| const textarea = document.querySelector("textarea"); | ||
|
|
||
| updateCharacterLimit(); | ||
|
|
||
| function updateCharacterLimit() { | ||
| const remainingCharacters = textarea.maxLength - textarea.value.length; | ||
| const charactersLeftP = document.querySelector("#character-limit-info"); | ||
| charactersLeftP.innerText = `You have ${remainingCharacters} characters remaining`; | ||
| console.log(remainingCharacters); | ||
| } | ||
|
|
||
| textarea.addEventListener("keyup", updateCharacterLimit); |
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.
You misunderstood the definition of these terms. Please lookup the definition of these angles and update your function accordingly.
You should also test the function on angle larger than or equal to 360.