-
-
Notifications
You must be signed in to change notification settings - Fork 386
London | 26-ITP-January | Mouawia Elkhalifa | Sprint 3 | Implement and Rewrite Tests #1011
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
Closed
MouawiaElkhalifa
wants to merge
6
commits into
CodeYourFuture:main
from
MouawiaElkhalifa:coursework/sprint-3-implement-and-rewrite
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e37d5b
Complete Sprint 3 implement and rewrite tests
MouawiaElkhalifa a2a1d2b
docs: explain ReferenceError in exercise 2
MouawiaElkhalifa d3d14ee
Removed misleading console log
MouawiaElkhalifa bb4c049
Fix: remove misleading console log from proper fraction tests
MouawiaElkhalifa 10a8373
Final fix: remove manual console log from card value tests
MouawiaElkhalifa 4ae83af
revert accidental sprint 1 change
MouawiaElkhalifa 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
79 changes: 64 additions & 15 deletions
79
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,52 +1,101 @@ | ||
| // This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck | ||
|
|
||
| // | ||
| // ORIGINAL QUESTION: | ||
| // 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♦". | ||
|
|
||
| // | ||
| // 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. | ||
|
|
||
| // | ||
| // When the card string is invalid (not following the above format), the function should | ||
| // throw an error. | ||
|
|
||
| // | ||
| // Acceptance criteria: | ||
| // After you have implemented the function, write tests to cover all the cases, and | ||
| // execute the code to ensure all tests pass. | ||
|
|
||
| function getCardValue(card) { | ||
| // TODO: Implement this function | ||
| // 1. Separate the rank from the suit emoji | ||
| const rank = card.slice(0, -1); | ||
| const suit = card.slice(-1); | ||
|
|
||
| // 2. Validate the suit | ||
| const validSuits = ["♠", "♥", "♦", "♣"]; | ||
| if (!validSuits.includes(suit)) { | ||
| throw new Error("Invalid suit"); | ||
| } | ||
|
|
||
| // 3. Calculate value based on the rank | ||
| if (rank === "A") { | ||
| return 11; | ||
| } | ||
|
|
||
| if (rank === "J" || rank === "Q" || rank === "K") { | ||
| return 10; | ||
| } | ||
|
|
||
| // 4. Handle number cards (2-10) | ||
| const numericRank = parseInt(rank, 10); | ||
|
|
||
| // numericRank.toString() === rank makes sure rank was really a clean number string | ||
| // Example: parseInt("9", 10) -> 9, "9" === "9" ✅ | ||
| // Example: parseInt("9x", 10) -> 9, "9" === "9x" ❌ (so invalid) | ||
| if (numericRank >= 2 && numericRank <= 10 && numericRank.toString() === rank) { | ||
| return numericRank; | ||
| } | ||
|
|
||
| // 5. If it reaches here, the rank was invalid | ||
| throw new Error("Invalid 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. | ||
| // Export for other test files (Jest step later) | ||
| module.exports = getCardValue; | ||
|
|
||
| // Helper functions to make our assertions easier to read. | ||
| // Helper function for testing (console.assert style) | ||
| 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: | ||
| // ======================= | ||
| // TESTS | ||
| // ======================= | ||
|
|
||
| // Test Aces and Face Cards | ||
| assertEquals(getCardValue("A♠"), 11); | ||
| assertEquals(getCardValue("J♥"), 10); | ||
| assertEquals(getCardValue("Q♦"), 10); | ||
| assertEquals(getCardValue("K♣"), 10); | ||
|
|
||
| // Test Numbers | ||
| assertEquals(getCardValue("9♠"), 9); | ||
| assertEquals(getCardValue("10♥"), 10); | ||
| assertEquals(getCardValue("2♣"), 2); | ||
|
|
||
| // Handling invalid cards | ||
| // Test Invalid Inputs (should throw errors) | ||
| try { | ||
| getCardValue("invalid"); | ||
| console.error("Error was not thrown for 'invalid'"); | ||
| } catch (e) {} | ||
|
|
||
| // This line will not be reached if an error is thrown as expected | ||
| console.error("Error was not thrown for invalid card"); | ||
| try { | ||
| getCardValue("1♠"); // There is no 1 card, only A,2..10,J,Q,K | ||
| console.error("Error was not thrown for '1♠'"); | ||
| } catch (e) {} | ||
|
|
||
| try { | ||
| getCardValue("A?"); // invalid suit | ||
| console.error("Error was not thrown for 'A?'"); | ||
| } catch (e) {} | ||
|
|
||
| // What other invalid card cases can you think of? | ||
| console.log("Tests complete!"); | ||
|
MouawiaElkhalifa marked this conversation as resolved.
Outdated
|
||
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
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
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.
This will run on every implementation run regardless if the tests have passed or not
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.
was lying. I’ve removed the manual success messages