Skip to content

Commit c43a321

Browse files
committed
commit again
1 parent 9542809 commit c43a321

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,21 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25+
const validSuits = ["♠", "♥", "♦", "♣"];
26+
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
27+
28+
const suit = card.slice(-1);
29+
const rank = card.slice(0, -1);
30+
2531

32+
if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
33+
throw new Error("Invalid card");
34+
}
35+
36+
if (rank === "A") return 11;
37+
if (["J", "Q", "K"].includes(rank)) return 10;
38+
39+
return Number(rank);
2640
}
2741

2842

@@ -42,17 +56,41 @@ function assertEquals(actualOutput, targetOutput) {
4256
// Examples 1 numbers:
4357
// Examples 1 numbers:
4458
assertEquals(getCardValue("9♠"), 9);
59+
assertEquals(getCardValue("5♣"), 5);
60+
61+
//Examples A:
62+
assertEquals(getCardValue("A♦"), 11)
63+
64+
//Examples J Q K:,
65+
assertEquals(getCardValue("Q♥"), 10)
66+
assertEquals(getCardValue("K♥"), 10)
67+
68+
//Examples invalid numbers:
69+
assertThrow(()=>getCardValue("1♥"))
70+
assertThrow(()=>getCardValue("11♥"))
71+
72+
//Examples invalid suit:
73+
assertThrow(()=>getCardValue("A*"))
74+
75+
//Examples missing rank or suit:
76+
assertThrow(()=>getCardValue("♥"))
77+
assertThrow(()=>getCardValue("5"))
78+
79+
//Example extra suit:
80+
assertThrow(()=>getCardValue("Q♠♠"))
4581

4682

4783
function assertThrow(fn){
48-
try { (fn)
84+
try { fn()
4985
// we try to run this function, if it throws, stop running this bit and run the catch below
5086

5187
// This line will not be reached if an error is thrown as expected
5288
console.error("Error was not thrown for invalid card");
5389
} catch (error) {
5490
// if the above code throws, we catch the error here, that stops the whole program crashing
55-
91+
console.log('there was an error getting card value!',error)
92+
}}
5693

94+
console.log(' we finished running getCardValue')
5795

5896
// What other invalid card cases can you think of?

0 commit comments

Comments
 (0)