|
22 | 22 | // execute the code to ensure all tests pass. |
23 | 23 |
|
24 | 24 | function getCardValue(card) { |
25 | | - // TODO: Implement this function |
| 25 | + const cardSuit = card.slice(card.length - 1); |
| 26 | + const cardRank = card.slice(0, card.length - 1); |
| 27 | + let cardValue; |
| 28 | + if ( |
| 29 | + ["♠", "♥", "♦", "♣"].includes(cardSuit) && |
| 30 | + ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"].includes( |
| 31 | + cardRank |
| 32 | + ) |
| 33 | + ) { |
| 34 | + if (cardRank == "1") { |
| 35 | + throw "invalid"; |
| 36 | + } |
| 37 | + |
| 38 | + switch (cardRank) { |
| 39 | + case "A": |
| 40 | + return 11; |
| 41 | + case "K": |
| 42 | + case "Q": |
| 43 | + case "J": |
| 44 | + return 10; |
| 45 | + //case "1": throw RangeError("invalid") |
| 46 | + default: |
| 47 | + return parseInt(cardRank); |
| 48 | + } |
| 49 | + } else { |
| 50 | + throw "invalid"; |
| 51 | + } |
26 | 52 | } |
27 | 53 |
|
28 | 54 | // The line below allows us to load the getCardValue function into tests in other files. |
29 | 55 | // This will be useful in the "rewrite tests with jest" step. |
30 | 56 | module.exports = getCardValue; |
31 | 57 |
|
32 | 58 | // 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 | | -} |
| 59 | +// function assertEquals(actualOutput, targetOutput) { |
| 60 | +// console.assert( |
| 61 | +// actualOutput === targetOutput, |
| 62 | +// `Expected ${actualOutput} to equal ${targetOutput}` |
39 | 63 |
|
40 | 64 | // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. |
41 | | -// Examples: |
42 | | -assertEquals(getCardValue("9♠"), 9); |
43 | | - |
44 | | -// Handling invalid cards |
45 | | -try { |
46 | | - getCardValue("invalid"); |
47 | | - |
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 | | - |
52 | | -// What other invalid card cases can you think of? |
| 65 | +//invalid card cases can you think of? |
0 commit comments