1- // This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
2-
3- // Implement a function getCardValue, when given a string representing a playing card,
1+ /// Implement a function getCardValue, when given a string representing a playing card,
42// should return the numerical value of the card.
53
64// A valid card string will contain a rank followed by the suit.
2220// execute the code to ensure all tests pass.
2321
2422function getCardValue ( card ) {
25- // TODO: Implement this function
26- }
23+ const rank = card . slice ( 0 , - 1 ) ;
24+
25+
26+
27+ if ( rank === "A" ) {
28+ return 11 ;
29+ }
30+ else if ( rank === "J" || rank === "Q" || rank === "K" ) {
31+ return 10 ;
32+ } ;
2733
34+
35+ const value = Number ( rank ) ;
36+
37+ if ( value >= 2 && value <= 10 ) {
38+ return value } ;
39+
40+ throw new Error ( "invalid card!" ) ;
41+ }
2842// The line below allows us to load the getCardValue function into tests in other files.
2943// This will be useful in the "rewrite tests with jest" step.
3044module . exports = getCardValue ;
@@ -39,7 +53,7 @@ function assertEquals(actualOutput, targetOutput) {
3953
4054// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
4155// Examples:
42- assertEquals ( getCardValue ( "9♠ " ) , 9 ) ;
56+ assertEquals ( getCardValue ( "9♥ " ) , 9 ) ;
4357
4458// Handling invalid cards
4559try {
5064} catch ( e ) {
5165 console . log ( "Error thrown for invalid card 🎉" ) ;
5266}
67+
5368
69+ try {
70+ getCardValue ( "null" ) ;
71+
72+ console . error ( "Error was not thrown for null card 😢" ) ;
73+ } catch ( e ) {
74+ console . log ( "Error thrown for invalid card 🎉" )
75+ }
76+
77+ try {
78+ getCardValue ( "0" ) ;
79+
80+ console . error ( "Error was not thrown for 0 card 😢" ) ;
81+ } catch ( e ) {
82+ console . log ( "Error thrown for invalid card 🎉" )
83+ }
5484// What other invalid card cases can you think of?
55- //for committing//
85+
0 commit comments