Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 1a4da93

Browse files
committed
get-card-value.js updated
1 parent 3dc1d86 commit 1a4da93

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

week-3/implement/get-card-value.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,32 @@
2929
// Given a card with an invalid rank (neither a number nor a recognized face card),
3030
// When the function is called with such a card,
3131
// Then it should throw an error indicating "Invalid card rank."
32+
//...........................................................
33+
34+
//ANSWER
35+
36+
37+
function getCardValue(card) {
38+
// first we need to extract the rank and leave the name
39+
const rank = card.slice(0, -1);
40+
// second we need to check if the card is a number card (2-10)
41+
if (/^[2-9]|10$/.test(rank)) {
42+
// Return the number value of the card
43+
return parseInt(rank);
44+
}
45+
// next we need to check if the card is (J, Q, K)
46+
else if (rank === "J" || rank === "Q" || rank === "K") {
47+
// these cards are worth 10 points
48+
return 10;
49+
}
50+
// then we check if the card is an Ace (A)
51+
else if (rank === "A") {
52+
// Aces are worth 11 points
53+
return 11;
54+
}
55+
// after that If the card is not recognized, show an error
56+
else {
57+
// Show an error message for cards we don't understand
58+
throw new Error("Invalid caRd.");
59+
}
60+
}

0 commit comments

Comments
 (0)