@@ -44,44 +44,51 @@ function getCardValue(card) {
4444 if ( rank === "J" || rank === "Q" || rank === "K" ) {
4545 return 10 ;
4646 }
47- }
47+ if ( Number ( rank ) >= 2 && Number ( rank ) <= 10 ) {
48+ return Number ( rank ) ;
49+ }
50+
51+ {
52+ throw new Error ( "Invalid card" ) ;
53+ }
4854
49- // The line below allows us to load the getCardValue function into tests in other files.
50- // This will be useful in the "rewrite tests with jest" step.
51- module . exports = getCardValue ;
55+ // The line below allows us to load the getCardValue function into tests in other files.
56+ // This will be useful in the "rewrite tests with jest" step.
57+ module . exports = getCardValue ;
5258
53- // Helper functions to make our assertions easier to read.
54- function assertEquals ( actualOutput , targetOutput ) {
55- console . assert (
56- actualOutput === targetOutput ,
57- `Expected ${ actualOutput } to equal ${ targetOutput } `
58- ) ;
59- }
59+ // Helper functions to make our assertions easier to read.
60+ function assertEquals ( actualOutput , targetOutput ) {
61+ console . assert (
62+ actualOutput === targetOutput ,
63+ `Expected ${ actualOutput } to equal ${ targetOutput } `
64+ ) ;
65+ }
66+
67+ // // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
68+ // // Examples:
69+ // assertEquals(getCardValue("9♠"), 9);
70+
71+ // // Handling invalid cards
72+ // try {
73+ // getCardValue("invalid");
6074
61- // // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
62- // // Examples:
63- // assertEquals(getCardValue("9♠"), 9);
64-
65- // // Handling invalid cards
66- // try {
67- // getCardValue("invalid");
68-
69- // // This line will not be reached if an error is thrown as expected
70- // console.error("Error was not thrown for invalid card 😢");
71- // } catch (e) {
72- // console.log("Error thrown for invalid card 🎉");
73- // }
74-
75- // // What other invalid card cases can you think of?
76- // //invalid rank
77- // getCardValue("1♠");
78- // getCardValue("11♣");
79- // getCardValue("0♦");
80- // getCardValue("B♥");
81- // getCardValue("X♠");
82-
83- // //invalid suit
84- // getCardValue("AX");
85- // getCardValue("5*");
86- // getCardValue("10H");
87- // getCardValue("QS");
75+ // // This line will not be reached if an error is thrown as expected
76+ // console.error("Error was not thrown for invalid card 😢");
77+ // } catch (e) {
78+ // console.log("Error thrown for invalid card 🎉");
79+ // }
80+
81+ // // What other invalid card cases can you think of?
82+ // //invalid rank
83+ // getCardValue("1♠");
84+ // getCardValue("11♣");
85+ // getCardValue("0♦");
86+ // getCardValue("B♥");
87+ // getCardValue("X♠");
88+
89+ // //invalid suit
90+ // getCardValue("AX");
91+ // getCardValue("5*");
92+ // getCardValue("10H");
93+ // getCardValue("QS");
94+ }
0 commit comments