3636
3737// ========================= getCardValue ===========================
3838
39- function getCardValue ( input ) {
40- const rank = input . slice ( 0 , - 1 ) ;
41- const suit = input . slice ( - 1 ) ;
39+ // function getCardValue(input) {
40+ // const rank = input.slice(0, -1);
41+ // const suit = input.slice(-1);
4242
4343
44- if ( input === "10" ) {
45- return 10 ;
46- } else if ( rank > 1 && rank < 11 ) {
47- return Number ( rank ) ;
48- }
49- else if ( input === "A" )
50- return 11 ;
51- else if ( input . toUpperCase ( ) === "K" || input . toUpperCase ( ) === "Q" || input . toUpperCase ( ) === "J" ) {
52- return 10 ;
53- }
44+ // if (input === "10"){
45+ // return 10;
46+ // }else if (rank > 1 && rank < 11) {
47+ // return Number(rank);
48+ // }
49+ // else if (input === "A")
50+ // return 11;
51+ // else if (input.toUpperCase() === "K" || input.toUpperCase() === "Q" || input.toUpperCase() === "J"){
52+ // return 10;
53+ // }
5454
55- if ( suit !== "♠" && suit !== "♥" && suit !== "♣" && suit !== "♦" && input . length > 1 ) {
56- return "Invalid card rank." ;
57- }
58- }
55+ // if (suit !== "♠" && suit !== "♥" && suit !== "♣" && suit !== "♦" && input.length > 1) {
56+ // return "Invalid card rank.";
57+ // }
58+ // }
5959
6060
6161// ======================== Test cases console.log() =====================
@@ -68,6 +68,43 @@ function getCardValue(input) {
6868// console.log(getCardValue("k")); // output 10
6969
7070
71+
72+
73+
74+ // ========================== optimized version =======================
75+
76+ function getCardValue ( input ) {
77+ // Define valid suits
78+ const validSuits = [ "♠" , "♥" , "♣" , "♦" ] ;
79+
80+ // Check if the input contains a valid suit emoji
81+ const suit = input . slice ( - 1 ) ;
82+ if ( ! validSuits . includes ( suit ) ) {
83+ return "Invalid card rank." ;
84+ }
85+
86+ // Extract rank (all characters except the last one)
87+ const rank = input . slice ( 0 , - 1 ) ;
88+
89+ // Check if the rank is a valid numeric value or face card
90+ if ( ! isNaN ( rank ) && Number ( rank ) >= 2 && Number ( rank ) <= 10 ) {
91+ return Number ( rank ) ; // Handle numeric cards (2-10)
92+ }
93+
94+ // Handle face cards (J, Q, K)
95+ if ( [ "J" , "Q" , "K" ] . includes ( rank . toUpperCase ( ) ) ) {
96+ return 10 ; // Face cards are worth 10
97+ }
98+
99+ // Handle Ace (A)
100+ if ( rank . toUpperCase ( ) === "A" ) {
101+ return 11 ; // Ace is worth 11
102+ }
103+
104+ // Handle invalid input (such as leading zeros, decimals, etc.)
105+ return "Invalid card rank." ;
106+ }
107+
71108// ======================== Test console.assert =====================
72109
73110console . assert ( getCardValue ( "10" ) === 10 , "Test case failed for input '10'" ) ;
@@ -77,44 +114,16 @@ console.assert(getCardValue("A") === 11, "Test case failed for input 'A'");
77114console . assert ( getCardValue ( "K" ) === 10 , "Test case failed for input 'K'" ) ;
78115console . assert ( getCardValue ( "3X" ) === "Invalid card rank." , "Test case failed for input '3X'" ) ;
79116
80- console . log ( "All tests passed!" ) ;
81-
82117
83- // ========================== optimized version =======================
84-
85- // function getCardValue(input) {
86- // const validSuits = ["♠", "♥", "♣", "♦"];
87- // const rank = input.length > 1 && isNaN(input) ? input.slice(0, -1) : input;
88- // const suit = input.length > 1 ? input.slice(-1) : null;
89-
90- // // Validate suit if present
91- // if (suit && !validSuits.includes(suit)) {
92- // return "Invalid card suit.";
93- // }
94-
95- // // Handle numeric cards
96- // if (rank >= 2 && rank <= 10) {
97- // return Number(rank);
98- // }
99-
100- // // Handle face cards
101- // if (["K", "Q", "J"].includes(rank.toUpperCase())) {
102- // return 10;
103- // }
104-
105- // // Handle Ace
106- // if (rank.toUpperCase() === "A") {
107- // return 11;
108- // }
118+ console . assert ( getCardValue ( "10♠" ) === 10 , "Test case failed for input '10♠'" ) ;
119+ console . assert ( getCardValue ( "5♥" ) === 5 , "Test case failed for input '5♥'" ) ;
120+ console . assert ( getCardValue ( "A" ) === 11 , "Test case failed for input 'A'" ) ;
121+ console . assert ( getCardValue ( "K" ) === 10 , "Test case failed for input 'K'" ) ;
122+ console . assert ( getCardValue ( "3X" ) === "Invalid card rank." , "Test case failed for input '3X'" ) ;
123+ console . assert ( getCardValue ( "010♠" ) === 10 , "Test case failed for input '010♠'" ) ;
124+ console . assert ( getCardValue ( "02♠" ) === 2 , "Test case failed for input '02♠'" ) ;
125+ console . assert ( getCardValue ( "0x02♠" ) === "Invalid card rank." , "Test case failed for input '0x02♠'" ) ;
126+ console . assert ( getCardValue ( "3.14♠" ) === "Invalid card rank." , "Test case failed for input '3.14♠'" ) ;
109127
110- // return "Invalid card rank.";
111- // }
128+ console . log ( "All tests passed!" ) ;
112129
113- // // Test cases
114- // console.log(getCardValue("10")); // Output: 10
115- // console.log(getCardValue("10♠")); // Output: 10
116- // console.log(getCardValue("5♥")); // Output: 5
117- // console.log(getCardValue("A")); // Output: 11
118- // console.log(getCardValue("K$")); // Output: "Invalid card suit."
119- // console.log(getCardValue("K")); // Output: 10
120- // console.log(getCardValue("11♥")); // Output: "Invalid card rank."
0 commit comments