Skip to content

Commit a447339

Browse files
added assertions for cardvalue-updated getCardValue function accordingly, created invalidCardRank function
1 parent d90591a commit a447339

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@
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+
const numberRank=Number(rank)
13+
if(numberRank>=2 && numberRank<10){
14+
return numberRank;
15+
}
16+
if(rank==="10" || rank==="J" || rank==="Q" || rank==="K"){
17+
return 10
18+
}
1119
if (rank === "A") {
1220
return 11;
1321
}
22+
return "Invalid card rank.";
1423
}
1524

1625
// The line below allows us to load the getCardValue function into tests in other files.
@@ -26,32 +35,53 @@ function assertEquals(actualOutput, targetOutput) {
2635
`Expected ${actualOutput} to equal ${targetOutput}`
2736
);
2837
}
38+
39+
function invalidCardRank(actualOutput, targetOutput) {
40+
console.assert(
41+
actualOutput === targetOutput,
42+
"Invalid card rank"
43+
);
44+
}
2945
// Acceptance criteria:
3046

31-
// 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),
47+
// 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
48+
// be a number 2-10, or one letter of J, Q, K, A),
3249
// When the function getCardValue is called with this card string as input,
3350
// Then it should return the numerical card value
34-
const aceofSpades = getCardValue("A♠");
35-
assertEquals(aceofSpades, 11);
51+
// const aceofSpades = getCardValue("A♠");
52+
// assertEquals(aceofSpades, 11);
3653

3754
// Handle Number Cards (2-10):
3855
// Given a card with a rank between "2" and "9",
3956
// When the function is called with such a card,
4057
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
4158
const fiveofHearts = getCardValue("5♥");
59+
assertEquals(fiveofHearts,5)
60+
61+
const sixofHearts = getCardValue("6♥");
62+
assertEquals(sixofHearts, 6);
4263
// ====> write your test here, and then add a line to pass the test in the function above
4364

4465
// Handle Face Cards (J, Q, K):
4566
// Given a card with a rank of "10," "J," "Q," or "K",
4667
// When the function is called with such a card,
4768
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
69+
const faceCards = getCardValue("J♥");
70+
assertEquals(faceCards,10)
71+
72+
assertEquals(getCardValue("Q♥"),10);
73+
4874

4975
// Handle Ace (A):
5076
// Given a card with a rank of "A",
5177
// When the function is called with an Ace,
5278
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
79+
const aceofSpades = getCardValue("A♠");
80+
assertEquals(aceofSpades, 11);
5381

5482
// Handle Invalid Cards:
5583
// Given a card with an invalid rank (neither a number nor a recognized face card),
5684
// When the function is called with such a card,
5785
// Then it should throw an error indicating "Invalid card rank."
86+
const invalidCard = getCardValue("m");
87+
invalidCardRank(invalidCard, "Invalid card rank.");

0 commit comments

Comments
 (0)