2222// execute the code to ensure all tests pass.
2323
2424function getCardValue(card) {
25- // TODO: Implement this function
25+ const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
26+ const validSuits = ["♠", "♥", "♦", "♣"];
27+
28+ let rank;
29+ let suit;
30+
31+ if (card.startsWith("10")) {
32+ rank = "10";
33+ suit = card.slice(2);
34+ } else {
35+ rank = card.slice(0, 1);
36+ suit = card.slice(1);
37+ }
38+
39+ if (!validRanks.includes(rank) || !validSuits.includes(suit)) {
40+ throw new Error(`Invalid card: ${card}`);
41+ }
42+
43+ if (rank === "A") {
44+ return 11;
45+ } else if (rank === "J" || rank === "Q" || rank === "K") {
46+ return 10;
47+ } else {
48+ return Number(rank);
49+ }
2650}
2751
2852// The line below allows us to load the getCardValue function into tests in other files.
@@ -40,15 +64,18 @@ function assertEquals(actualOutput, targetOutput) {
4064// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4165// Examples:
4266assertEquals(getCardValue("9♠"), 9);
67+ assertEquals(getCardValue("A♥"), 11);
68+ assertEquals(getCardValue("K♦"), 10);
69+ assertEquals(getCardValue("10♣"), 10);
4370
4471// Handling invalid cards
4572try {
4673 getCardValue("invalid");
4774
4875 // This line will not be reached if an error is thrown as expected
49- console.error("Error was not thrown for invalid card 😢 ");
76+ console.error("Error was not thrown for invalid card 😞 ");
5077} catch (e) {
5178 console.log("Error thrown for invalid card 🎉");
5279}
5380
54- // What other invalid card cases can you think of?
81+ // What other invalid card cases can you think of?
0 commit comments