|
1 | | -// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck |
| 1 | +/* |
| 2 | +This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck |
2 | 3 |
|
3 | | -// Don't forget to write tests in implement-tests-with-node-test. |
4 | | - |
5 | | -// Implement a function getCardValue, when given a string representing a playing card, |
6 | | -// should return the numerical value of the card. |
7 | | - |
8 | | -// A valid card string will contain a rank followed by the suit. |
9 | | -// The rank can be one of the following strings: |
10 | | -// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" |
11 | | -// The suit can be one of the following emojis: |
12 | | -// "♠", "♥", "♦", "♣" |
13 | | -// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦". |
14 | | - |
15 | | -// When the card is an ace ("A"), the function should return 11. |
16 | | -// When the card is a face card ("J", "Q", "K"), the function should return 10. |
17 | | -// When the card is a number card ("2" to "10"), the function should return its numeric value. |
18 | | - |
19 | | -// When the card string is invalid (not following the above format), the function should |
20 | | -// throw an error. |
21 | | - |
22 | | -// Acceptance criteria: |
23 | | -// After you have implemented the function, write tests to cover all the cases, and |
24 | | -// execute the code to ensure all tests pass. |
| 4 | +Implement a function getCardValue, when given a string representing a playing card, |
| 5 | +should return the numerical value of the card. |
| 6 | +A valid card string will contain a rank followed by the suit. |
| 7 | +The rank can be one of the following strings: |
| 8 | + "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" |
| 9 | +The suit can be one of the following emojis: |
| 10 | + "♠", "♥", "♦", "♣" |
| 11 | +For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦". |
| 12 | +When the card is an ace ("A"), the function should return 11. |
| 13 | +When the card is a face card ("J", "Q", "K"), the function should return 10. |
| 14 | +When the card is a number card ("2" to "10"), the function should return its numeric value. |
| 15 | +When the card string is invalid (not following the above format), the function should |
| 16 | +throw an error. |
| 17 | +Acceptance criteria: |
| 18 | +After you have implemented the function, write tests to cover all the cases, and |
| 19 | +execute the code to ensure all tests pass. |
| 20 | +*/ |
25 | 21 |
|
26 | 22 | export function getCardValue(card) { |
27 | | - // TODO: Implement this function |
28 | 23 | const cardValue = card.slice(0,-1); |
| 24 | + const cardSuit = card.slice(-1); |
| 25 | + const validSuits = ["♠", "♥", "♦", "♣"]; |
| 26 | + const validNumberCards = ["2", "3", "4", "5", "6", "7", "8", "9", "10"] |
| 27 | + const validFaceCards = ["A", "K", "Q", "J"]; |
29 | 28 | const cardValueNumber = Number(cardValue) |
30 | | - if (cardValue === "A") { |
31 | | - return 11 |
32 | | - } |
33 | | - else if (cardValue === "K" || cardValue === "Q" || cardValue === "J") { |
34 | | - return 10 |
| 29 | + if (!validNumberCards.includes(cardValue) && !validFaceCards.includes(cardValue)){ |
| 30 | + throw new Error(`Expected a number followed by a suit, but got "${card}"`); |
| 31 | + } else if (!validSuits.includes(cardSuit)) { |
| 32 | + throw new Error(`Expected card suit to be: "♠", "♥", "♦", "♣" but got "${card}"`); |
35 | 33 | } |
36 | | - else if (cardValueNumber >= 2 && cardValueNumber <= 10) { |
37 | | - return cardValueNumber |
| 34 | + if (cardValue === "A") { |
| 35 | + return 11; |
| 36 | + } else if (validFaceCards.includes(cardValue)) { |
| 37 | + return 10; |
| 38 | + } else { |
| 39 | + return cardValueNumber; |
| 40 | + } |
38 | 41 | } |
39 | | - throw new Error(`Expected a number followed by a suit, but got "${card}"`); |
40 | | -} |
|
0 commit comments