66
77// Acceptance criteria:
88
9- // Given a card string in the format "A♠" (representing a card in blackjack -
9+ // 1. Given a card string in the format "A♠" (representing a card in blackjack -
1010// the last character will always be an emoji for a suit, and all characters
1111// before will be a number 2-10, or one letter of J, Q, K, A),
1212// When the function getCardValue is called with this card string as input,
1313// Then it should return the numerical card value
1414
15- // Handle Number Cards (2-10):
15+ // 2. Handle Number Cards (2-10):
1616// Given a card with a rank between "2" and "9",
1717// When the function is called with such a card,
18- // Then it should return the numeric value corresponding to the rank
18+ // Then it should return the numeric value corresponding to the rank
1919// (e.g., "5" should return 5).
2020
21- // Handle Face Cards (J, Q, K):
21+ // 3. Handle Face Cards (J, Q, K):
2222// Given a card with a rank of "10," "J," "Q," or "K",
2323// When the function is called with such a card,
2424// Then it should return the value 10, as these cards are worth 10 points each
2525// in blackjack.
2626
27- // Handle Ace (A):
27+ // 4. Handle Ace (A):
2828// Given a card with a rank of "A",
2929// When the function is called with an Ace,
3030// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
3131
32- // Handle Invalid Cards:
32+ // 5. Handle Invalid Cards:
3333// Given a card with an invalid rank (neither a number nor a recognized face card),
3434// When the function is called with such a card,
3535// Then it should throw an error indicating "Invalid card rank."
3636
37+ // ========================= getCardValue ===========================
3738
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 ( ) ;
39+ function getCardValue ( input ) {
40+ const rank = input . slice ( 0 , - 1 ) ;
4141
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 = [ "♠" , "♥" , "♣" , "♦" ] ;
42+ // console.log(rank)
43+ // return rank//fist character
4744
48- if ( faceCards . includes ( chain ) ) {
45+ if ( input === "10" )
4946 return 10 ;
47+
48+ else if ( rank > 1 && rank < 11 ) {
49+ return Number ( rank ) ;
5050 }
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- }
51+ else if ( input === "A" )
52+ return 11
5753
58- if ( chain === ace ) {
59- return 11 ;
54+ else if (
55+ input . toUpperCase ( ) === "K" ||
56+ input . toUpperCase ( ) === "Q" ||
57+ input . toUpperCase ( ) === "J"
58+ ) {
59+ return 10 ;
6060 }
61- //Handle Invalid Cards:
62- throw new Error ( "Invalid card rank." ) ; // throw does no return values only parameter that does not include the functions
61+ else return " Invalid card rank." ;
62+
6363}
6464
65- console . log ( IdentifyNumberCard ( 2 ) ) ;
65+
66+ // // Test cases
67+ console . log ( getCardValue ( "10" ) ) ; // Output: 10
68+ console . log ( getCardValue ( "10♠" ) ) ; // Output: 10
69+ console . log ( getCardValue ( "5♥" ) ) ; // Output: 5
70+ console . log ( getCardValue ( "A" ) ) ; // Output: 11
71+ console . log ( getCardValue ( "k$" ) ) ; // output Invalid card rank.
72+ console . log ( getCardValue ( "k" ) ) ; // output 10
73+
74+
75+
76+
77+ // ========================== optimized version =======================
78+
79+ // function getCardValue(input) {
80+ // const validSuits = ["♠", "♥", "♣", "♦"];
81+ // const rank = input.length > 1 && isNaN(input) ? input.slice(0, -1) : input;
82+ // const suit = input.length > 1 ? input.slice(-1) : null;
83+
84+ // // Validate suit if present
85+ // if (suit && !validSuits.includes(suit)) {
86+ // return "Invalid card suit.";
87+ // }
88+
89+ // // Handle numeric cards
90+ // if (rank >= 2 && rank <= 10) {
91+ // return Number(rank);
92+ // }
93+
94+ // // Handle face cards
95+ // if (["K", "Q", "J"].includes(rank.toUpperCase())) {
96+ // return 10;
97+ // }
98+
99+ // // Handle Ace
100+ // if (rank.toUpperCase() === "A") {
101+ // return 11;
102+ // }
103+
104+ // return "Invalid card rank.";
105+ // }
106+
107+ // // Test cases
108+ // console.log(getCardValue("10")); // Output: 10
109+ // console.log(getCardValue("10♠")); // Output: 10
110+ // console.log(getCardValue("5♥")); // Output: 5
111+ // console.log(getCardValue("A")); // Output: 11
112+ // console.log(getCardValue("K$")); // Output: "Invalid card suit."
113+ // console.log(getCardValue("K")); // Output: 10
114+ // console.log(getCardValue("11♥")); // Output: "Invalid card rank."
115+
116+
0 commit comments