Skip to content

Commit e6d384c

Browse files
committed
Editing getCardValue Code
1 parent ed40750 commit e6d384c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@
88
// write one test at a time, and make it pass, build your solution up methodically
99
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
1010
function getCardValue(card) {
11+
const rank = card.slice(0, -1);
12+
if (["2", "3", "4", "5", "6", "7", "8", "9"].includes(rank)) {
13+
return parseInt(rank, 10);
14+
}
1115
if (rank === "A") {
1216
return 11;
1317
}
18+
if (["K", "Q", "J", "10"].includes(rank)) {
19+
return 10;
20+
}
21+
return "Invalid card rank";
1422
}
1523

1624
// The line below allows us to load the getCardValue function into tests in other files.
@@ -39,19 +47,41 @@ assertEquals(aceofSpades, 11);
3947
// When the function is called with such a card,
4048
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4149
const fiveofHearts = getCardValue("5♥");
50+
assertEquals(fiveofHearts, 5);
4251
// ====> write your test here, and then add a line to pass the test in the function above
4352

4453
// Handle Face Cards (J, Q, K):
4554
// Given a card with a rank of "10," "J," "Q," or "K",
4655
// When the function is called with such a card,
4756
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
4857

58+
const kingofDiamonds = getCardValue("K♦");
59+
assertEquals(kingofDiamonds, 10);
60+
const queenofClubs = getCardValue("Q♣");
61+
assertEquals(queenofClubs, 10);
62+
const jackofHearts = getCardValue("J♥");
63+
assertEquals(jackofHearts, 10);
64+
const tenofSpades = getCardValue("10♠");
65+
assertEquals(tenofSpades, 10);
4966
// Handle Ace (A):
5067
// Given a card with a rank of "A",
5168
// When the function is called with an Ace,
5269
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
5370

71+
const aceofClubs = getCardValue("A♣");
72+
assertEquals(aceofClubs, 11);
73+
5474
// Handle Invalid Cards:
5575
// Given a card with an invalid rank (neither a number nor a recognized face card),
5676
// When the function is called with such a card,
5777
// Then it should throw an error indicating "Invalid card rank."
78+
const invalidCard = getCardValue("1♠");
79+
assertEquals(invalidCard, "Invalid card rank");
80+
const anotherInvalidCard = getCardValue("B♦");
81+
assertEquals(anotherInvalidCard, "Invalid card rank");
82+
83+
console.log(getCardValue("3♠"));
84+
console.log(getCardValue("10♣"));
85+
console.log(getCardValue("J♦"));
86+
console.log(getCardValue("A♥"));
87+
console.log(getCardValue("11♠"));

0 commit comments

Comments
 (0)