Skip to content

Commit 4a61d73

Browse files
committed
adding conditon to handle differents options
1 parent ca012a7 commit 4a61d73

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66

77
// Acceptance criteria:
88

9-
// 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),
9+
// Given a card string in the format "A♠" (representing a card in blackjack -
10+
// the last character will always be an emoji for a suit, and all characters
11+
// before will be a number 2-10, or one letter of J, Q, K, A),
1012
// When the function getCardValue is called with this card string as input,
1113
// Then it should return the numerical card value
1214

1315
// Handle Number Cards (2-10):
1416
// Given a card with a rank between "2" and "9",
1517
// When the function is called with such a card,
16-
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
18+
// Then it should return the numeric value corresponding to the rank
19+
// (e.g., "5" should return 5).
1720

1821
// Handle Face Cards (J, Q, K):
1922
// Given a card with a rank of "10," "J," "Q," or "K",
2023
// When the function is called with such a card,
21-
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
24+
// Then it should return the value 10, as these cards are worth 10 points each
25+
// in blackjack.
2226

2327
// Handle Ace (A):
2428
// Given a card with a rank of "A",
@@ -29,3 +33,33 @@
2933
// Given a card with an invalid rank (neither a number nor a recognized face card),
3034
// When the function is called with such a card,
3135
// Then it should throw an error indicating "Invalid card rank."
36+
37+
38+
function IdentifyNumberCard (input){
39+
//we validate and convert into str and upperCase before to compare with our local var
40+
const chain = input.toString().toUpperCase();
41+
42+
//Option that we want to check
43+
const faceCards = ["K", "J", "Q", "10"];
44+
const ace = "A";
45+
const numberCards = [1, 2, 3, 4, 5, 6, 7, 8, 9];
46+
const suits = ["♠", "♥", "♣", "♦"];
47+
48+
if (faceCards.includes(chain)) {
49+
return 10;
50+
}
51+
//!isNaN(chain) ensures that chain is a valid number or a string representation of a number.
52+
//to validate the string and verify is a number or string ("abc")=> false, ("1")=>true
53+
//!isNaN is not a number? true is number. && convert the chain into number and compare if the input is the NumberCard
54+
if (!isNaN(chain) && numberCards.includes(Number(chain))) {
55+
return Number(chain);
56+
}
57+
58+
if (chain === ace) {
59+
return 11;
60+
}
61+
//Handle Invalid Cards:
62+
throw new Error("Invalid card rank."); // throw does no return values only parameter that does not include the functions
63+
}
64+
65+
console.log(IdentifyNumberCard(2));

0 commit comments

Comments
 (0)