-
-
Notifications
You must be signed in to change notification settings - Fork 335
Manchester | 26-ITP-Jan | Liban Jama | Sprint 3 | Implement and rewrite tests #1197
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 5 commits
1924ef4
a73fa1a
3d438c6
f6f394c
254abc7
1a4482e
aabad30
c555833
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 |
|---|---|---|
|
|
@@ -22,7 +22,35 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| const suits = ["♠", "♥", "♦", "♣"]; // valid card suits | ||
|
|
||
| let suit = ""; | ||
| let rank = card; | ||
|
|
||
| if (card.length > 1) { | ||
| suit = card.slice(-1); // gives me the last character and saves it in the suit variable. | ||
| rank = card.slice(0, -1); // gets beginning character up to (not including) the last character and saves it in the rank variable. | ||
| } | ||
|
|
||
| if (suit && !suits.includes(suit)) { | ||
| throw new Error("Error invalid card"); | ||
| } | ||
|
|
||
| // if card is an ace return 11 | ||
| if (rank === "A") { | ||
| return 11; | ||
| } | ||
|
|
||
| // if card is face "J", "Q", "K" | ||
| else if (rank === "J" || rank === "Q" || rank === "K") { | ||
| return 10; | ||
| } | ||
|
|
||
| // if card is a number card and card is bigger than 2 and less than 10 ("2" to "10"), should return its numerical value | ||
| else if (Number(rank) >= 2 && Number(rank) <= 10) { | ||
| return Number(rank); | ||
|
||
| } | ||
| throw new Error("Error invalid card"); | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,34 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { | |
| }); | ||
|
|
||
| // Case 2: Right angle | ||
| test(`should return "Right angle" when (angle === 90)`, () => { | ||
| // Test various right angles, including boundary cases | ||
| expect(getAngleType(90)).toEqual("Right angle"); | ||
| }); | ||
| // Case 3: Obtuse angles | ||
| test(`should return "Obtuse angles" when (angle > 90 && < 180)`, () => { | ||
| // Test various obtuse angles, including boundary cases | ||
| expect(getAngleType(96)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(142)).toEqual("Obtuse angle"); | ||
| expect(getAngleType(178)).toEqual("Obtuse angle"); | ||
| }); | ||
| // Case 4: Straight angle | ||
| test(`should return "Straight angle" when (angle === 180)`, () => { | ||
| // Test various straight angles, including boundary cases | ||
| expect(getAngleType(180)).toEqual("Straight angle"); | ||
| }); | ||
| // Case 5: Reflex angles | ||
| test(`should return "Reflex angles" when (angle > 180 && < 360)`, () => { | ||
| // Test various reflex angles, including boundary cases | ||
| expect(getAngleType(199)).toEqual("Reflex angle"); | ||
| expect(getAngleType(245)).toEqual("Reflex angle"); | ||
| expect(getAngleType(306)).toEqual("Reflex angle"); | ||
| }); | ||
| // Case 6: Invalid angles | ||
| test(`should return "Invalid angles" when (angle is =<0 &&>=360)`, () => { | ||
|
||
| // Test various invalid angles, including boundary cases | ||
| expect(getAngleType(400)).toEqual("Invalid angle"); | ||
| expect(getAngleType(-7)).toBe("Invalid angle"); | ||
| expect(getAngleType(360)).toBe("Invalid angle"); | ||
| expect(getAngleType(0)).toBe("Invalid angle"); | ||
| }); | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
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. Have you tested your implementation with this script?
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. yes and it passed
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. Unless you have different files on your computer, I don't see how your function can pass this test. Both the function implementation and the tests have bugs.
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. ok ill fix those bugs, thanks
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. Altered function to pass failing test. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,27 @@ test(`Should return 11 when given an ace card`, () => { | |
| // Face Cards (J, Q, K) | ||
| // Invalid Cards | ||
|
|
||
| // Case 2: Face (J, Q, K) | ||
| test(`Should return 10 when given an face card`, () => { | ||
| expect(getCardValue("J♣")).toEqual(10); | ||
| }); | ||
|
|
||
| test(`Should return 10 when given an face card`, () => { | ||
| expect(getCardValue("Q♦")).toEqual(10); | ||
| }); | ||
|
|
||
| test(`Should return 10 when given an face card`, () => { | ||
| expect(getCardValue("K♥")).toEqual(10); | ||
| }); | ||
| // Case 3: Number Cards (2-10) | ||
| test(`Should return numerical value when given an number card`, () => { | ||
| expect(getCardValue("5")).toEqual(5); | ||
| }); | ||
|
Comment on lines
+29
to
+32
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. Supposedly a valid card is a string in this format: "5" (without the suit character), would be considered an invalid card.
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, ive changed that.
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. Function implementation is correct. This test category is supposed for valid number cards. |
||
|
|
||
| test(`Should return error if the card string is invalid`, () => { | ||
| expect(() => getCardValue("17")).toThrow("Error invalid card"); | ||
| }); | ||
|
|
||
| // To learn how to test whether a function throws an error as expected in Jest, | ||
| // please refer to the Jest documentation: | ||
| // https://jestjs.io/docs/expect#tothrowerror | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.