2222// execute the code to ensure all tests pass.
2323
2424function getCardValue ( card ) {
25- let rank = card . slice ( 0 , - 1 ) ;
25+ let rankStr = card . slice ( 0 , - 1 ) ;
26+ let rank = Number ( rankStr ) ;
27+ console . log ( "rank :" + rank ) ;
2628 let suit = card . slice ( - 1 ) ;
29+ console . log ( "suit :" + suit ) ;
2730 const suitArray = [ "♠" , "♥" , "♦" , "♣" ] ;
28- if ( ! suitArray . includes ( suit ) ) throw new Error ( "Invalid card" ) ;
29- if ( rank === "A" ) return 11 ;
30- if ( rank === "Q" || rank === "K" || rank === "J" ) return 10 ;
31- if ( Number ( rank ) >= 2 && Number ( rank ) <= 10 ) return Number ( rank ) ;
32- throw new Error ( "Invalid card" ) ;
31+ if ( ! suitArray . includes ( suit ) || ! / ^ ( 1 0 | [ 2 - 9 ] ) $ / . test ( rankStr ) ) {
32+ throw new Error ( "Invalid card" ) ;
33+ } else if ( rank === "A" ) {
34+ return 11 ;
35+ } else if ( rank === "Q" || rank === "K" || rank === "J" ) {
36+ return 10 ;
37+ } else if ( rank >= 2 && rank <= 10 && Number . isInteger ( rank ) ) {
38+ return Number ( rank ) ;
39+ } else {
40+ throw new Error ( "Invalid card" ) ;
41+ }
3342}
3443
44+ console . log ( getCardValue ( "0002♠" ) ) ;
45+
3546// The line below allows us to load the getCardValue function into tests in other files.
3647// This will be useful in the "rewrite tests with jest" step.
3748module . exports = getCardValue ;
@@ -46,16 +57,16 @@ function assertEquals(actualOutput, targetOutput) {
4657
4758// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4859// Examples:
49- assertEquals ( getCardValue ( "A♠" ) , 11 ) ;
60+ /* assertEquals(getCardValue("A♠"), 11);
5061assertEquals(getCardValue("A♣"), 11);
5162assertEquals(getCardValue("Q♠"), 10);
5263assertEquals(getCardValue("K♣"), 10);
5364assertEquals(getCardValue("J♦"), 10);
5465assertEquals(getCardValue("2♠"), 2);
55- assertEquals ( getCardValue ( "10♣" ) , 10 ) ;
66+ assertEquals(getCardValue("10♣"), 10);*/
5667
5768// Handling invalid cards
58- try {
69+ /* try {
5970 getCardValue("AX");
6071 console.error("Error was not thrown for invalid card 😢");
6172} catch (e) {
8091 console.error("Error was not thrown for invalid card 😢");
8192} catch (e) {
8293 console.log("Error thrown for invalid card 🎉");
83- }
94+ }*/
8495
8596// What other invalid card cases can you think of?
0 commit comments