Counting Cards JS exercise -- Can you explain what's happening?

Counting Cards JS exercise -- Can you explain what's happening?
0

#1

I passed the “Counting Cards” exercise (as part of the “Basic JS” track, but I can’t say that I actually UNDERSTAND what’s happening.

So… if cc(2), then it increments by 1, since it corresponds to case 2? and so forth for cc(‘A’), it would decrement by 1 since it corresponds to case A? sorry - i’m just having trouble understanding how this works.

This is my code:

var count = 0;

function cc(card) {
  switch(card) {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      count++;
      break;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
      count--;    
  }
  
  if(count > 0) {
    return count + " Bet";
  } else {
    return count + " Hold";    
  }

  cc(2); cc(3); cc(7); cc('K'); cc('A');

#2

NEVERMIND - I understand now how it works!


#3

Nice!

I’ve also edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#4

I am not able to complete a task because of the space in Bet and Hold.:grinning:


#5

When you concatenate count with the player's decision (Bet or Hold) you should add a space between the first double quote and the player's decision, like this: `count + " Bet"`or `count + " Hold"`. This way you get count Bet or count Hold.

As you can see in the lesson Concatenating Strings with Plus Operator:

Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you’ll
need to add them yourself.

So, if you write count + "Bet" you get countBet, if you write count + " Bet" you get count Bet.

I hope it helps.