-
-
Notifications
You must be signed in to change notification settings - Fork 337
Manchester | 26-ITP-Jan | Mehroz Munir | Sprint 3 | Implement and Rewrite tests #1118
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 1 commit
04a8f07
2a9ffe9
267e816
1b1293e
760e0f0
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,32 @@ | |
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| // Handling invalid cards | ||
| try{ | ||
| const rank = card.slice(0,-1); //rank of the card is everything except the last character of card string | ||
| const suit = card.slice(-1); // suit is the last character of the card string | ||
| if((card.length != 2 && card.length !=3) || !isValidCard(rank, suit)){ | ||
| throw new Error("Invalid card"); | ||
| } | ||
| if(rank === "J" || rank === "Q" || rank == "K") | ||
| return 10 | ||
| else if(rank == "A") | ||
| return 11; | ||
| else | ||
| return Number(rank); | ||
| } | ||
| catch(e){ | ||
| return e.message; | ||
| } | ||
|
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. This requirement has not yet been met:
Currently your function returns a string when a card is invalid. Note: The error is supposed to be thrown to the caller.
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 that's right I was not throwing an error. I have fixed it now.
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. I have created a separate function with the name assertThrows in the 3-get-card-value.js file to catch the error. But, I am not sure how can I test this thrown error in 3-get-card-value.test.js file where I am using jest test.
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. See lines 43-45 in
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. I should have looked at this. Fixed now. |
||
| } | ||
|
|
||
| function isValidCard(rank,suit){ | ||
| const ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; | ||
| const suits = ["♠", "♥", "♦", "♣"]; | ||
| if(ranks.includes(rank) && suits.includes(suit)) | ||
| return true; | ||
| else | ||
| return false; | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -41,12 +66,14 @@ function assertEquals(actualOutput, targetOutput) { | |
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| } catch (e) {} | ||
|
|
||
| // What other invalid card cases can you think of? | ||
| assertEquals(getCardValue("10♠"), 10); | ||
| assertEquals(getCardValue("Q♠"), 10); | ||
| assertEquals(getCardValue("A♣"), 11); | ||
| assertEquals(getCardValue("2♠"), 2); | ||
| assertEquals(getCardValue("J♦"), 10); | ||
| assertEquals(getCardValue("K♠"), 10); | ||
| assertEquals(getCardValue("Invalid"), "Invalid card"); | ||
| assertEquals(getCardValue("1Q"), "Invalid card"); | ||
| assertEquals(getCardValue("-10♦"), "Invalid card"); | ||
| assertEquals(getCardValue("♦K"), "Invalid card"); | ||
| assertEquals(getCardValue("Q♦♦"), "Invalid card"); | ||
|
cjyuan marked this conversation as resolved.
|
Uh oh!
There was an error while loading. Please reload this page.