|
1 | 1 | // This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck |
2 | 2 |
|
3 | | -// Don't forget to write tests in implement-tests-with-node-test. |
4 | | - |
5 | 3 | // Implement a function getCardValue, when given a string representing a playing card, |
6 | 4 | // should return the numerical value of the card. |
7 | 5 |
|
|
23 | 21 | // After you have implemented the function, write tests to cover all the cases, and |
24 | 22 | // execute the code to ensure all tests pass. |
25 | 23 |
|
26 | | -export function getCardValue(card) { |
27 | | - // TODO: Implement this function |
| 24 | +function getCardValue(card) { |
| 25 | + let regex = /^([2-9]|10|[AJQK])[♠♥♣♦]$/; |
| 26 | + const valid = regex.test(card); |
| 27 | + if (!valid) { |
| 28 | + throw new Error("Invalid card"); |
| 29 | + } |
| 30 | + let rank = card.slice(0, -1); |
| 31 | + |
| 32 | + if (rank === "A") { |
| 33 | + return 11; |
| 34 | + } else if (rank === "J" || rank === "Q" || rank === "K") { |
| 35 | + return 10; |
| 36 | + } else { |
| 37 | + rank = Number(rank); |
| 38 | + return rank; |
| 39 | + } |
| 40 | +} |
| 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 | +assertEquals(getCardValue("A♠"), 11); |
| 58 | +assertEquals(getCardValue("10♥"), 10); |
| 59 | +assertEquals(getCardValue("2♥"), 2); |
| 60 | +assertEquals(getCardValue("K♠"), 10); |
| 61 | +assertEquals(getCardValue("J♣"), 10); |
| 62 | +assertEquals(getCardValue("Q♦"), 10); |
| 63 | + |
| 64 | +// Handling invalid cards |
| 65 | +try { |
| 66 | + getCardValue("invalid"); |
| 67 | + |
| 68 | + // This line will not be reached if an error is thrown as expected |
| 69 | + console.error("Error was not thrown for invalid card 😢"); |
| 70 | +} catch (e) { |
| 71 | + console.log("Error thrown for invalid card 🎉"); |
| 72 | +} |
| 73 | + |
| 74 | +// What other invalid card cases can you think of? |
| 75 | + |
| 76 | +try { |
| 77 | + getCardValue(3); |
| 78 | + console.error("Error was not thrown for number 3"); |
| 79 | +} catch (e) { |
| 80 | + console.log("Error thrown for invalid format"); |
| 81 | +} |
| 82 | + |
| 83 | +try { |
| 84 | + getCardValue("11K♠"); |
| 85 | + console.error("Error format too long"); |
| 86 | +} catch (e) { |
| 87 | + console.log("Error thrown for too many characters"); |
| 88 | +} |
| 89 | + |
| 90 | +try { |
| 91 | + getCardValue(-1); |
| 92 | + console.error("Error for negative number"); |
| 93 | +} catch (e) { |
| 94 | + console.log("Error thrown for negative number"); |
| 95 | +} |
| 96 | + |
| 97 | +try { |
| 98 | + getCardValue("22♥"); |
| 99 | + console.error("Error for string 22♥"); |
| 100 | +} catch (e) { |
| 101 | + console.log("Error thrown for string 22♥"); |
28 | 102 | } |
0 commit comments