You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js
+36-4Lines changed: 36 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,21 @@
8
8
// write one test at a time, and make it pass, build your solution up methodically
9
9
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
10
10
functiongetCardValue(card){
11
+
constrank=card.slice(0,-1);
12
+
13
+
if(/^[JQK]$/.test(rank)){
14
+
return10;
15
+
}
16
+
11
17
if(rank==="A"){
12
18
return11;
13
19
}
20
+
21
+
if(/^[2-9]$/.test(rank)||rank==="10"){
22
+
returnNumber(rank);
23
+
}
24
+
25
+
thrownewError("Invalid card rank.");
14
26
}
15
27
16
28
// The line below allows us to load the getCardValue function into tests in other files.
@@ -31,27 +43,47 @@ function assertEquals(actualOutput, targetOutput) {
31
43
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
32
44
// When the function getCardValue is called with this card string as input,
33
45
// Then it should return the numerical card value
34
-
constaceofSpades=getCardValue("A♠");
35
-
assertEquals(aceofSpades,11);
46
+
constaceOfSpades=getCardValue("A♠");
47
+
assertEquals(aceOfSpades,11);
36
48
37
49
// Handle Number Cards (2-10):
38
50
// Given a card with a rank between "2" and "9",
39
51
// When the function is called with such a card,
40
52
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
41
-
constfiveofHearts=getCardValue("5♥");
42
-
// ====> write your test here, and then add a line to pass the test in the function above
53
+
constfiveOfHearts=getCardValue("5♥");
54
+
assertEquals(fiveOfHearts,5);
43
55
44
56
// Handle Face Cards (J, Q, K):
45
57
// Given a card with a rank of "10," "J," "Q," or "K",
46
58
// When the function is called with such a card,
47
59
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
60
+
consttenOfClubs=getCardValue("10♣");
61
+
assertEquals(tenOfClubs,10);
62
+
63
+
constjackOfDiamonds=getCardValue("J♦");
64
+
assertEquals(jackOfDiamonds,10);
65
+
66
+
constkingOfHearts=getCardValue("K♥");
67
+
assertEquals(kingOfHearts,10);
68
+
69
+
constqueenOfSpades=getCardValue("Q♠");
70
+
assertEquals(queenOfSpades,10);
48
71
49
72
// Handle Ace (A):
50
73
// Given a card with a rank of "A",
51
74
// When the function is called with an Ace,
52
75
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
76
+
constaceOfDiamonds=getCardValue("A♦");
77
+
assertEquals(aceOfDiamonds,11);
53
78
54
79
// Handle Invalid Cards:
55
80
// Given a card with an invalid rank (neither a number nor a recognized face card),
56
81
// When the function is called with such a card,
57
82
// Then it should throw an error indicating "Invalid card rank."
0 commit comments