|
| 1 | +/// Implement a function getCardValue, when given a string representing a playing card, |
| 2 | +// should return the numerical value of the card. |
| 3 | + |
| 4 | +// A valid card string will contain a rank followed by the suit. |
| 5 | +// The rank can be one of the following strings: |
| 6 | +// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" |
| 7 | +// The suit can be one of the following emojis: |
| 8 | +// "♠", "♥", "♦", "♣" |
| 9 | +// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦". |
| 10 | + |
| 11 | +// When the card is an ace ("A"), the function should return 11. |
| 12 | +// When the card is a face card ("J", "Q", "K"), the function should return 10. |
| 13 | +// When the card is a number card ("2" to "10"), the function should return its numeric value. |
| 14 | + |
| 15 | +// When the card string is invalid (not following the above format), the function should |
| 16 | +// throw an error. |
| 17 | + |
| 18 | +// Acceptance criteria: |
| 19 | +// After you have implemented the function, write tests to cover all the cases, and |
| 20 | +// execute the code to ensure all tests pass. |
| 21 | + |
| 22 | +function getCardValue(card) { |
| 23 | + const rank = card.slice(0, -1); |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + if (rank === "A"){ |
| 28 | + return 11 ; |
| 29 | + } |
| 30 | + else if (rank === "J" || rank === "Q" || rank === "K"){ |
| 31 | + return 10 ; |
| 32 | + }; |
| 33 | + |
| 34 | + |
| 35 | +const value = Number(rank); |
| 36 | + |
| 37 | + if( value>=2 && value <= 10 ){ |
| 38 | + return value}; |
| 39 | + |
| 40 | + throw new Error("invalid card!"); |
| 41 | +} |
| 42 | +// The line below allows us to load the getCardValue function into tests in other files. |
| 43 | +// This will be useful in the "rewrite tests with jest" step. |
| 44 | +module.exports = getCardValue; |
| 45 | + |
| 46 | +// Helper functions to make our assertions easier to read. |
| 47 | +function assertEquals(actualOutput, targetOutput) { |
| 48 | + console.assert( |
| 49 | + actualOutput === targetOutput, |
| 50 | + `Expected ${actualOutput} to equal ${targetOutput}` |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. |
| 55 | +// Examples: |
| 56 | +assertEquals(getCardValue("9♥"), 9); |
| 57 | + |
| 58 | +// Handling invalid cards |
| 59 | +try { |
| 60 | + getCardValue("invalid"); |
| 61 | + |
| 62 | + // This line will not be reached if an error is thrown as expected |
| 63 | + console.error("Error was not thrown for invalid card 😢"); |
| 64 | +} catch (e) { |
| 65 | + console.log("Error thrown for invalid card 🎉"); |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +try { |
| 70 | + getCardValue("null"); |
| 71 | + |
| 72 | + console.error("Error was not thrown for null card 😢"); |
| 73 | +} catch (e) { |
| 74 | + console.log("Error thrown for invalid card 🎉") |
| 75 | +} |
| 76 | + |
| 77 | +try { |
| 78 | + getCardValue("0"); |
| 79 | + |
| 80 | + console.error("Error was not thrown for 0 card 😢"); |
| 81 | +} catch (e) { |
| 82 | + console.log("Error thrown for invalid card 🎉") |
| 83 | +} |
| 84 | +// What other invalid card cases can you think of? |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
0 commit comments