Skip to content

Commit d364d62

Browse files
committed
implement getCardValue with JSDoc and console.assert tests for all card types
1 parent 11254cc commit d364d62

1 file changed

Lines changed: 81 additions & 5 deletions

File tree

Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,49 @@
2121
// After you have implemented the function, write tests to cover all the cases, and
2222
// execute the code to ensure all tests pass.
2323

24+
/**
25+
* Returns the numerical value of a playing card.
26+
*
27+
* A standard deck has 4 suits: ♠ (spades), ♥ (hearts), ♦ (diamonds), ♣ (clubs)
28+
* Each card is written as a rank followed by a suit symbol e.g. "A♠", "10♥", "K♦"
29+
*
30+
* @param {string} card - A string representing a playing card e.g. "A♠", "10♥", "K♦", "7♣"
31+
* @returns {number} - The numerical value of the card:
32+
* - Ace ("A") returns 11
33+
* - Face cards ("J", "Q", "K") return 10
34+
* - Number cards ("2" to "10") return their numeric value
35+
* @throws {Error} - If the card string is not in a valid format e.g. "invalid" or "1♠"
36+
*
37+
* @example
38+
* getCardValue("A♠") // returns 11
39+
* getCardValue("10♥") // returns 10
40+
* getCardValue("K♦") // returns 10
41+
* getCardValue("7♣") // returns 7
42+
*/
2443
function getCardValue(card) {
2544
// TODO: Implement this function
45+
// NEW: The suit symbol (♠ ♥ ♦ ♣) always takes up 2 characters in JavaScript
46+
// slice(0, -2) means "give me everything except the last 2 characters"
47+
// so "A♠" becomes "A", "10♥" becomes "10", "K♦" becomes "K"
48+
// MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
49+
const rank = card.slice(0, -1);
50+
51+
// ✨ NEW: Ace is always worth 11
52+
if (rank === "A") return 11;
53+
54+
// ✨ NEW: Face cards - Jack, Queen and King are all worth 10
55+
if (rank === "J" || rank === "Q" || rank === "K") return 10;
56+
57+
// ✨ NEW: Number() converts a string like "9" into the actual number 9
58+
// so we can check if it falls in the valid range of 2 to 10
59+
// MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
60+
const numValue = Number(rank);
61+
if (numValue >= 2 && numValue <= 10) return numValue;
62+
63+
// ✨ NEW: If we reach this line, none of the above matched
64+
// throw stops the function and sends an error back to whoever called it
65+
// MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw
66+
throw new Error(`Invalid card: ${card}`);
2667
}
2768

2869
// The line below allows us to load the getCardValue function into tests in other files.
@@ -38,17 +79,52 @@ function assertEquals(actualOutput, targetOutput) {
3879
}
3980

4081
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
41-
// Examples:
42-
assertEquals(getCardValue("9♠"), 9);
82+
83+
// Ace cards - always 11 regardless of suit
84+
assertEquals(getCardValue("A♠"), 11);
85+
assertEquals(getCardValue("A♥"), 11);
86+
87+
// Number cards - return their face value as a number
88+
assertEquals(getCardValue("2♦"), 2); // boundary: lowest number card
89+
assertEquals(getCardValue("9♣"), 9);
90+
assertEquals(getCardValue("10♥"), 10); // boundary: highest number card
91+
92+
// Face cards - Jack, Queen and King all return 10
93+
assertEquals(getCardValue("J♣"), 10);
94+
assertEquals(getCardValue("Q♦"), 10);
95+
assertEquals(getCardValue("K♠"), 10);
4396

4497
// Handling invalid cards
98+
// try/catch lets us test that an error IS thrown
99+
// try: runs the code. catch: catches the error if one is thrown
100+
// MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
45101
try {
46102
getCardValue("invalid");
47-
48103
// This line will not be reached if an error is thrown as expected
49-
console.error("Error was not thrown for invalid card 😢");
104+
console.error("Error was not thrown for invalid card ");
50105
} catch (e) {
51-
console.log("Error thrown for invalid card 🎉");
106+
console.log("Error thrown for invalid card ");
52107
}
53108

54109
// What other invalid card cases can you think of?
110+
111+
try {
112+
getCardValue("1♠"); // 1 is not a valid rank - valid ranks are 2 through A
113+
console.error("Error was not thrown for rank 1 ");
114+
} catch (e) {
115+
console.log("Error thrown for rank 1 ");
116+
}
117+
118+
try {
119+
getCardValue("11♥"); // 11 is not a valid rank - valid ranks are 2 through A
120+
console.error("Error was not thrown for rank 11 ");
121+
} catch (e) {
122+
console.log("Error thrown for rank 11 ");
123+
}
124+
125+
try {
126+
getCardValue("A"); // missing suit
127+
console.error("Error was not thrown for missing suit ");
128+
} catch (e) {
129+
console.log("Error thrown for missing suit ");
130+
}

0 commit comments

Comments
 (0)