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
+56-22Lines changed: 56 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -8,26 +8,60 @@
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);// get the rank (before the suit symbol)
11
+
if(typeofcard!=="string"){
12
+
thrownewError("Card must be a string")
13
+
}
12
14
15
+
if(card.length<2){
16
+
thrownewError("Invalid card rank")
17
+
}
13
18
14
-
constnum=Number(rank);// converting to a number.
19
+
constrank=card.slice(0,-1);
20
+
constsuit=card.slice(-1);
15
21
16
-
if(!isNaN(rank)){
17
-
returnnum;// Number card
22
+
if(!["♠","♥","♦","♣"].includes(suit)){
23
+
thrownewError("Invalid card rank");
18
24
}
19
25
26
+
if(rank==="A")return11;// Ace
27
+
28
+
29
+
30
+
//const rank = card.slice(0, -1); // get the rank (before the suit symbol)
31
+
32
+
33
+
// const num = Number(rank); // converting to a number.
34
+
35
+
// if (!isNaN(rank)) {
36
+
// return num; // Number card
37
+
// }
38
+
20
39
if(rank==="J"||rank==="Q"||rank==="K"){
21
40
return10;// Face cards
22
41
}
23
42
24
-
if(rank==="A")return11;// Ace
43
+
if(!/^[0-9]+$/.test(rank)){
44
+
thrownewError("Invalid card rank");
45
+
}
46
+
// Reject weird or malformed ranks (like 3AAAA)
47
+
48
+
constnum=Number(rank);
49
+
// Convert numeric string
50
+
51
+
if(num>=2&&num<=10){
52
+
returnnum;
53
+
}
54
+
// Accept valid 2–10 range only
55
+
56
+
57
+
25
58
26
59
// Anything else is invalid
27
60
thrownewError("Invalid card rank")
28
61
29
62
}
30
63
64
+
31
65
console.log(Number("0x002"));
32
66
console.log(Number("2.1"));
33
67
console.log(Number("9e1"));
@@ -62,31 +96,31 @@ function assertEquals(actualOutput, targetOutput) {
62
96
// 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),
63
97
// When the function getCardValue is called with this card string as input,
64
98
// Then it should return the numerical card value
65
-
constaceofSpades=getCardValue("A♠");
66
-
assertEquals(aceofSpades,11);
99
+
constaceOfSpades=getCardValue("A♠");
100
+
assertEquals(aceOfSpades,11);
67
101
68
102
// Handle Number Cards (2-10):
69
103
// Given a card with a rank between "2" and "9",
70
104
// When the function is called with such a card,
71
105
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
72
-
constfiveofHearts=getCardValue("5♥");
73
-
assertEquals(fiveofHearts,5);
106
+
constfiveOfHearts=getCardValue("5♥");
107
+
assertEquals(fiveOfHearts,5);
74
108
75
-
consttenofDiamonds=getCardValue("10♦");
76
-
assertEquals(tenofDiamonds,10);
109
+
consttenOfDiamonds=getCardValue("10♦");
110
+
assertEquals(tenOfDiamonds,10);
77
111
78
112
// Handle Face Cards (J, Q, K):
79
113
// Given a card with a rank of "10," "J," "Q," or "K",
80
114
// When the function is called with such a card,
81
115
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
0 commit comments