-
-
Notifications
You must be signed in to change notification settings - Fork 336
London | 26-Jan-ITP | Shuheda Begum | Sprint 3 | Implement and Rewrite Test #1071
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 |
|---|---|---|
|
|
@@ -21,8 +21,47 @@ | |
| // After you have implemented the function, write tests to cover all the cases, and | ||
| // execute the code to ensure all tests pass. | ||
|
|
||
| // TODO: Implement this function | ||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| // Valid suits | ||
| const validSuits = ["♠", "♥", "♦", "♣"]; | ||
|
|
||
| // Card must be at least 2 characters (e.g., "A♠") | ||
| if (typeof card !== "string" || card.length < 2) { | ||
| throw new Error("Invalid card"); | ||
| } | ||
|
|
||
| // Suit is always the last character | ||
|
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. Your comments are also good, but for one line code, it might not be necessary, but still it's good practice👍
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. Thank you. I did extra notes so I can refer back to it and understand what I did. |
||
| const suit = card.slice(-1); | ||
|
|
||
| // Rank is everything except the last character | ||
| const rank = card.slice(0, -1); | ||
|
|
||
| // Validate suit | ||
| if (!validSuits.includes(suit)) { | ||
| throw new Error("Invalid card"); | ||
| } | ||
|
|
||
| // Handle Ace | ||
| if (rank === "A") { | ||
| return 11; | ||
| } | ||
|
|
||
| // Handle face cards | ||
| if (rank === "J" || rank === "Q" || rank === "K") { | ||
| return 10; | ||
| } | ||
|
|
||
| // Handle number cards (2–10) | ||
| const number = Number(rank); | ||
|
|
||
| if (number >= 2 && number <= 10) { | ||
| return number; | ||
| } | ||
|
|
||
| // If nothing matched → invalid | ||
| throw new Error("Invalid card"); | ||
|
|
||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
|
|
@@ -38,15 +77,47 @@ function assertEquals(actualOutput, targetOutput) { | |
| } | ||
|
|
||
| // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. | ||
| // Test: Ace, face cards, number cards, invalid suit, invalid rank, completely invalid string. | ||
| // Examples: | ||
| // Number cards: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| assertEquals(getCardValue("2♠"), 2); | ||
| assertEquals(getCardValue("10♦"), 10); | ||
|
|
||
| // Face card: | ||
| assertEquals(getCardValue("J♣"), 10); | ||
| assertEquals(getCardValue("Q♦"), 10); | ||
| assertEquals(getCardValue("K♠"), 10); | ||
|
|
||
| // Ace | ||
| assertEquals(getCardValue("A♥"), 11); | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
| console.error("Error was not thrown for invalid string"); | ||
| } catch (e) {} | ||
|
|
||
| // Invalid rank | ||
| try { | ||
| getCardValue("1♠"); | ||
| console.error("Error wasnot thrown for invalid rank"); | ||
| } catch (e) {} | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| // Invalid suit | ||
| try { | ||
| getCardValue("A?"); | ||
| console.error("Error wasnot thrown for invalid suit"); | ||
| } catch (e) {} | ||
|
|
||
| // Missing suit | ||
| try { | ||
| getCardValue("A"); | ||
| console.error("Error wasnot thrown for missing suit"); | ||
| } catch (e) {} | ||
|
|
||
| console.log("All tests executed."); | ||
|
|
||
|
|
||
| // What other invalid card cases can you think of? | ||
| // Test: Ace, face cards, number cards, invalid suit, invalid rank, completely invalid string. | ||
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.
Good error check here!