|
20 | 20 | // Acceptance criteria: |
21 | 21 | // After you have implemented the function, write tests to cover all the cases, and |
22 | 22 | // execute the code to ensure all tests pass. |
23 | | -function getCardValue(card) { |
24 | | - const rank = card.slice(0, -1); |
25 | | - const suit = card.slice(-1); |
26 | 23 |
|
27 | | - const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; |
| 24 | +function getCardValue(card) { |
| 25 | + // TODO: Implement this function |
| 26 | +} |
28 | 27 |
|
29 | | - const validSuits = ["♠", "♥", "♦", "♣"]; |
| 28 | +// The line below allows us to load the getCardValue function into tests in other files. |
| 29 | +// This will be useful in the "rewrite tests with jest" step. |
| 30 | +module.exports = getCardValue; |
30 | 31 |
|
31 | | - if (!validRanks.includes(rank) || !validSuits.includes(suit)) { |
32 | | - throw new Error("Invalid card"); |
33 | | - } |
| 32 | +// Helper functions to make our assertions easier to read. |
| 33 | +function assertEquals(actualOutput, targetOutput) { |
| 34 | + console.assert( |
| 35 | + actualOutput === targetOutput, |
| 36 | + `Expected ${actualOutput} to equal ${targetOutput}` |
| 37 | + ); |
| 38 | +} |
34 | 39 |
|
35 | | - if (rank === "A") { |
36 | | - return 11; |
37 | | - } |
| 40 | +// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. |
| 41 | +// Examples: |
| 42 | +assertEquals(getCardValue("9♠"), 9); |
38 | 43 |
|
39 | | - if (["J", "Q", "K"].includes(rank)) { |
40 | | - return 10; |
41 | | - } |
| 44 | +// Handling invalid cards |
| 45 | +try { |
| 46 | + getCardValue("invalid"); |
42 | 47 |
|
43 | | - return Number(rank); |
| 48 | + // This line will not be reached if an error is thrown as expected |
| 49 | + console.error("Error was not thrown for invalid card 😢"); |
| 50 | +} catch (e) { |
| 51 | + console.log("Error thrown for invalid card 🎉"); |
44 | 52 | } |
45 | 53 |
|
46 | | -module.exports = getCardValue; |
| 54 | +// What other invalid card cases can you think of? |
0 commit comments