|
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 generateValidCards() { |
24 | | - const ranks = "A,2,3,4,5,6,7,8,9,10,J,Q,K".split(","); |
25 | | - const suites = "♠,♥,♦,♣".split(","); |
26 | | - const validCards = new Set(); |
27 | | - for (const rank of ranks) { |
28 | | - for (const suite of suites) { |
29 | | - validCards.add(rank + suite); |
30 | | - } |
31 | | - } |
32 | | - return validCards; |
33 | | -} |
34 | 23 |
|
35 | 24 | function getCardValue(card) { |
36 | 25 | // TODO: Implement this function |
37 | | - const validCards = generateValidCards(); |
38 | | - if (!validCards.has(card)) { |
39 | | - throw new Error("invalid"); |
| 26 | + const cardRanks = "A,2,3,4,5,6,7,8,9,10,J,Q,K".split(","); |
| 27 | + const cardSuits = "♠,♥,♦,♣".split(","); |
| 28 | + |
| 29 | + // Throw error if card is empty |
| 30 | + if (!card) throw new Error(`Invalid card`); |
| 31 | + |
| 32 | + const cardSuit = card[card.length - 1]; |
| 33 | + // Throw error is card suit is not valid |
| 34 | + if (!cardSuits.includes(cardSuit)) { |
| 35 | + throw new Error(`Invalid card suit`); |
40 | 36 | } |
41 | 37 |
|
42 | | - let cardValue = card.slice(0, -1); |
43 | | - if (cardValue === "A") return 11; |
44 | | - if (cardValue === "J" || cardValue === "Q" || cardValue === "K") return 10; |
45 | | - return Number(cardValue); |
| 38 | + const cardRank = card.slice(0, -1); |
| 39 | + // Throw error is cardRank is not valid |
| 40 | + if (!cardRanks.includes(cardRank)) { |
| 41 | + throw new Error(`Invalid card rank`); |
| 42 | + } |
| 43 | + |
| 44 | + // Return the card value based on given rank |
| 45 | + if (cardRank === "A") return 11; |
| 46 | + if (["J", "Q", "K"].includes(cardRank)) return 10; |
| 47 | + return Number(cardRank); |
46 | 48 | } |
47 | 49 |
|
48 | 50 | // The line below allows us to load the getCardValue function into tests in other files. |
49 | 51 | // This will be useful in the "rewrite tests with jest" step. |
50 | 52 | module.exports = getCardValue; |
51 | 53 |
|
52 | 54 | // Helper functions to make our assertions easier to read. |
53 | | -function assertEquals(card, targetOutput) { |
| 55 | +function assertEquals(actualOutput, targetOutput) { |
| 56 | + console.assert( |
| 57 | + actualOutput === targetOutput, |
| 58 | + `Expected ${actualOutput} to equal ${targetOutput}` |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. |
| 63 | +// Examples: |
| 64 | +assertEquals(getCardValue("9♠"), 9); |
| 65 | +assertEquals(getCardValue("J♥"), 10); |
| 66 | +assertEquals(getCardValue("Q♣"), 10); |
| 67 | +assertEquals(getCardValue("K♠"), 10); |
| 68 | +assertEquals(getCardValue("A♥"), 11); |
| 69 | +assertEquals(getCardValue("10♦"), 10); |
| 70 | + |
| 71 | +// Handling invalid cards |
| 72 | +function assertErrors(func, errorMessage) { |
54 | 73 | try { |
55 | | - console.log(`Card: ${card}`); |
56 | | - const actualOutput = getCardValue(card); |
| 74 | + func(); |
| 75 | + console.error("Error was not thrown for invalid card 😢"); |
| 76 | + } catch (error) { |
57 | 77 | console.assert( |
58 | | - actualOutput === targetOutput, |
59 | | - `Expected ${actualOutput} to equal ${targetOutput}` |
| 78 | + error.message.includes(errorMessage), |
| 79 | + `Expected error: ${errorMessage}, got: ${error.message}` |
60 | 80 | ); |
61 | | - console.error("Error was not thrown for invalid card 😢"); |
62 | | - } catch (e) { |
63 | 81 | console.log("Error thrown for invalid card 🎉"); |
64 | 82 | } |
65 | | - console.log("-".repeat(45)); |
66 | 83 | } |
67 | 84 |
|
68 | | -// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. |
69 | | -assertEquals("9♠", 9); |
70 | | -assertEquals("A", 11); |
71 | | -assertEquals("Q", 10); |
72 | | -assertEquals("5", 5); |
73 | | -assertEquals("A♣", 11); |
74 | | -assertEquals("Q♣", 11); |
75 | | -assertEquals("A♦", 10); |
| 85 | +function testInvalidCards() { |
| 86 | + const invalidCardCases = [ |
| 87 | + ["A", "Invalid card suit"], |
| 88 | + ["11♠", "Invalid card rank"], |
| 89 | + ["N♠", "Invalid card rank"], |
| 90 | + ["7K", "Invalid card suit"], |
| 91 | + ["", "Invalid card"], |
| 92 | + ]; |
| 93 | + |
| 94 | + invalidCardCases.forEach(([card, message]) => { |
| 95 | + console.log("-".repeat(50)); |
| 96 | + console.log(`Card: ${card}, Message: ${message}`); |
| 97 | + assertErrors(() => getCardValue(card), message); |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +testInvalidCards(); |
0 commit comments