|
36 | 36 |
|
37 | 37 | // ========================= getCardValue =========================== |
38 | 38 |
|
39 | | -function getCardValue (input){ |
| 39 | +function getCardValue(input) { |
40 | 40 | const rank = input.slice(0, -1); |
| 41 | + const suit = input.slice(-1); |
41 | 42 |
|
42 | | - // console.log(rank) |
43 | | - // return rank//fist character |
44 | 43 |
|
45 | | - if (input === "10") |
| 44 | + if (input === "10"){ |
46 | 45 | return 10; |
47 | | - |
48 | | - else if (rank > 1 && rank < 11) { |
| 46 | + }else if (rank > 1 && rank < 11) { |
49 | 47 | return Number(rank); |
50 | | - } |
51 | | - else if(input === "A") |
52 | | - return 11 |
53 | | - |
54 | | - else if ( |
55 | | - input.toUpperCase() === "K" || |
56 | | - input.toUpperCase() === "Q" || |
57 | | - input.toUpperCase() === "J" |
58 | | - ) { |
| 48 | + } |
| 49 | + else if (input === "A") |
| 50 | + return 11; |
| 51 | + else if (input.toUpperCase() === "K" || input.toUpperCase() === "Q" || input.toUpperCase() === "J"){ |
59 | 52 | return 10; |
60 | 53 | } |
61 | | - else return "Invalid card rank."; |
62 | 54 |
|
| 55 | + if (suit !== "♠" && suit !== "♥" && suit !== "♣" && suit !== "♦" && input.length > 1) { |
| 56 | + return "Invalid card rank."; |
| 57 | + } |
63 | 58 | } |
64 | 59 |
|
65 | 60 |
|
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 |
| 61 | +// ======================== Test cases console.log() ===================== |
| 62 | + |
| 63 | +// console.log(getCardValue("10")); // Output: 10 |
| 64 | +// console.log(getCardValue("10♠")); // Output: 10 |
| 65 | +// console.log(getCardValue("5♥")); // Output: 5 |
| 66 | +// console.log(getCardValue("A")); // Output: 11 |
| 67 | +// console.log(getCardValue("k$")); // output Invalid card rank. |
| 68 | +// console.log(getCardValue("k")); // output 10 |
73 | 69 |
|
74 | 70 |
|
| 71 | +// ======================== Test console.assert ===================== |
| 72 | + |
| 73 | +console.assert(getCardValue("10") === 10, "Test case failed for input '10'"); |
| 74 | +console.assert(getCardValue("10♠") === 10, "Test case failed for input '10♠'"); |
| 75 | +console.assert(getCardValue("5♥") === 5, "Test case failed for input '5♥'"); |
| 76 | +console.assert(getCardValue("A") === 11, "Test case failed for input 'A'"); |
| 77 | +console.assert(getCardValue("K") === 10, "Test case failed for input 'K'"); |
| 78 | +console.assert(getCardValue("3X") === "Invalid card rank.", "Test case failed for input '3X'"); |
| 79 | + |
| 80 | +console.log("All tests passed!"); |
75 | 81 |
|
76 | 82 |
|
77 | 83 | // ========================== optimized version ======================= |
@@ -112,5 +118,3 @@ console.log(getCardValue("k")); // output 10 |
112 | 118 | // console.log(getCardValue("K$")); // Output: "Invalid card suit." |
113 | 119 | // console.log(getCardValue("K")); // Output: 10 |
114 | 120 | // console.log(getCardValue("11♥")); // Output: "Invalid card rank." |
115 | | - |
116 | | - |
|
0 commit comments