[null, {"document_url": "https://www.freecodecamp.org/forum/t/card-counting-challenge/219132", "unformatted_src": "//discourse-user-assets.s3.amazonaws.com/optimized/3X/3/b/3bfbc95b78dfbcd544e0ef28f2dbb12d895f073f_1_690x388.png", "src": "http://discourse-user-assets.s3.amazonaws.com/optimized/3X/3/b/3bfbc95b78dfbcd544e0ef28f2dbb12d895f073f_1_690x388.png", "formatted_filename": "bfbc b dfbcd e ef f dbb d f f", "alt_text": "image", "rendered_width": 690, "rendered_height": 388}, null, null]
Card-counting challenge

SeanFl42 2018-08-18 21:35:26 UTC #1


I’ve managed the first three card sequences (2 3 4 5 6, 7 8 9 and 10 J Q K A) without much ado but the subsequent sequences have proven more challenging. Neither a set of if/else if/else statements nor a switch statement wouldn’t work on those.


Here’s my code: image.png1920x1080 219 KB

Thanks in advance for any help.

Cody_Biggs 2018-08-18 21:44:16 UTC #2

Before you continue trying to solve. Where are you updating the variable count like it tells you?

AndrewFan0408 2018-08-18 21:44:36 UTC #3

I’m sorry, but I don’t think you fully understand what this challenge want you to do.
For case 2, 3, 4, 5, 6, it wants you to add 1 to the global variable count, and for 10 ‘j’ ‘q’ ‘k’ ‘a’, you need to -1 in count

AndrewFan0408 2018-08-18 21:45:53 UTC #4

And then, you need to check the count and return count and “bet” if count > 0 else return hold if count <= 0;

AndrewFan0408 2018-08-18 21:49:07 UTC #5

check this link Counting Cards Challenge - let's compare our code!

I used a switch instead of if-else, since there’s only a few values card can take, and there’s only 2 scenarios to consider: incrementing or decrementing count. There’s more lines, but I think writing this using if-else can get ugly quickly (like: (card == 10 || card == 'J' || card == 'Q' || ...)) function cc(card) { // Only change code below this line switch (card) { case 2: case 3: case 4: case 5: case 6: count++; break; case 10: case 'J': case…

SeanFl42 2018-08-19 00:44:58 UTC #6


I checked the link and compared that person’s code with my own. It seems I’ve over-thought this. I didn’t use count. My mistake. There was no need to include cases 7-9 in my switch statement because the count wasn’t supposed to change in those cases. My original if statement seems to have over-complicated the task, perhaps through excessive nesting (six layers). The other person’s if statement used a lot less code than mine. The return statements weren’t supposed to have a number, just the count variable + " Bet" if the count was incremented or " Hold" if the count was decremented.

Thank you for your help. I’ve ticked Solution under your last post.