-
-
Notifications
You must be signed in to change notification settings - Fork 391
Birmingham | 26-ITP-May | Ogbemi Mene | Sprint 3 | Coursework with jest #1482
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
Open
meneogbemi42-bit
wants to merge
7
commits into
CodeYourFuture:main
Choose a base branch
from
meneogbemi42-bit:coursework/sprint3-r
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 75 additions & 33 deletions
108
Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,96 @@ | ||
| // This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck | ||
|
|
||
| // Implement a function getCardValue, when given a string representing a playing card, | ||
| // should return the numerical value of the card. | ||
|
|
||
| // A valid card string will contain a rank followed by the suit. | ||
| // The rank can be one of the following strings: | ||
| // "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" | ||
| // The suit can be one of the following emojis: | ||
| // "♠", "♥", "♦", "♣" | ||
| // For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦". | ||
| function getCardValue(card) { | ||
| // Validate that input is a non-empty string and has at least 2 characters (Rank + Suit) | ||
| if (typeof card !== "string" || card.length < 2) { | ||
| throw new Error("Invalid card format"); | ||
| } | ||
|
|
||
| // Define valid suits | ||
| const validSuits = ["♠", "♥", "♦", "♣"]; | ||
|
|
||
| // When the card is an ace ("A"), the function should return 11. | ||
| // When the card is a face card ("J", "Q", "K"), the function should return 10. | ||
| // When the card is a number card ("2" to "10"), the function should return its numeric value. | ||
| // The suit is always the last character/emoji of the string. | ||
| // Using Array.from() or string methods safely extracts it. | ||
| const suit = card.slice(-1); | ||
| if (!validSuits.includes(suit)) { | ||
| throw new Error("Invalid card suit"); | ||
| } | ||
|
|
||
| // When the card string is invalid (not following the above format), the function should | ||
| // throw an error. | ||
| // The rank is everything up to the suit emoji. | ||
| const rank = card.slice(0, -1); | ||
|
|
||
| // Acceptance criteria: | ||
| // After you have implemented the function, write tests to cover all the cases, and | ||
| // execute the code to ensure all tests pass. | ||
| // Handle value mappings | ||
| if (rank === "A") { | ||
| return 11; | ||
| } | ||
|
|
||
| if (["J", "Q", "K"].includes(rank)) { | ||
| return 10; | ||
| } | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| // Parse numeric ranks ("2" through "10") | ||
| const numericValue = parseInt(rank, 10); | ||
| if (!isNaN(numericValue) && numericValue >= 2 && numericValue <= 10 && String(numericValue) === rank) { | ||
|
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. What does |
||
| return numericValue; | ||
| } | ||
|
|
||
| // If the rank doesn't match any criteria, it's invalid. | ||
| throw new Error("Invalid card rank"); | ||
| } | ||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
| // This will be useful in the "rewrite tests with jest" step. | ||
| module.exports = getCardValue; | ||
|
|
||
| // Helper functions to make our assertions easier to read. | ||
| // ========================================== | ||
| // ASSERTION HELPERS | ||
| // ========================================== | ||
|
|
||
| function assertEquals(actualOutput, targetOutput) { | ||
| console.assert( | ||
| actualOutput === targetOutput, | ||
| `Expected ${actualOutput} to equal ${targetOutput}` | ||
| ); | ||
| } | ||
|
|
||
| // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. | ||
| // Examples: | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| // A helper to verify that an invalid input throws an error as expected | ||
| function assertThrows(invalidCard) { | ||
| try { | ||
| getCardValue(invalidCard); | ||
| console.error(`❌ Error was NOT thrown for invalid card: "${invalidCard}"`); | ||
| } catch (e) { | ||
| // Test passes if an error is thrown | ||
| } | ||
| } | ||
|
|
||
| // Handling invalid cards | ||
| try { | ||
| getCardValue("invalid"); | ||
| // ========================================== | ||
| // TEST SUITE | ||
| // ========================================== | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card 😢"); | ||
| } catch (e) { | ||
| console.log("Error thrown for invalid card 🎉"); | ||
| } | ||
| console.log("Running tests..."); | ||
|
|
||
| // 1. Valid Aces (Value: 11) | ||
| assertEquals(getCardValue("A♠"), 11); | ||
| assertEquals(getCardValue("A♥"), 11); | ||
|
|
||
| // 2. Valid Face Cards (Value: 10) | ||
| assertEquals(getCardValue("J♣"), 10); | ||
| assertEquals(getCardValue("Q♦"), 10); | ||
| assertEquals(getCardValue("K♠"), 10); | ||
|
|
||
| // 3. Valid Numeric Boundary Cards | ||
| assertEquals(getCardValue("2♥"), 2); | ||
| assertEquals(getCardValue("5♦"), 5); | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| assertEquals(getCardValue("10♣"), 10); | ||
|
|
||
| // 4. Invalid Card Scenarios (Should throw errors) | ||
| assertThrows("invalid"); // Completely wrong format | ||
| assertThrows("A"); // Missing suit | ||
| assertThrows("♠"); // Missing rank | ||
| assertThrows("1♠"); // 1 is not a valid rank (should be "A") | ||
| assertThrows("11♥"); // Out-of-bounds number card | ||
| assertThrows("A♣️"); // Suit variations or hidden characters | ||
| assertThrows("Q⭐️"); // Invalid suit emoji | ||
| assertThrows("J ♠"); // Unwanted spacing | ||
|
|
||
| // What other invalid card cases can you think of? | ||
| console.log("All tests completed!"); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could you combine this condition with the returned expression below?