How do you not count 7, 8, or 9 in the counting cards exercise and why do you use count as a variable (instead of card)?

Continuing the discussion from freeCodeCamp Challenge Guide: Counting Cards:

{ switch(card) {case 1:
                                 break;
  case 2:
                                 break;
  case 3:
                                 break;
  case 4:
                                 break;
  case 5:
                                 break;
  case 6:
           count++;                      
      break;
  
   case 7:
      break;
   case 8:
      break;
   case 9:
      card = '';
    break;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
      card = -1;
  if (card > 0) {return "bet";} 
  else {return "hold";}     
  return card;               }

I assume this is what is holding me back. Iā€™m trying to avoid looking at the answer, but had to for the count partā€¦ and now I want to understand the logic of this

By the way, this method of asking why has helped me with solving novel problems independently, so I greatly appreciate the help.

3 Likes

Also, why is one able not to use break between each case in this example? I noticed the example does not have break between each case.

 case 9:
      card = '';

What is going on here? Why are you setting the argument (card) to an empty string? Similarly, are you sure that you are supposed to set card to negative one for J, Q, K, and A.

Every block where you have

case <something>:
    break;

You are setting your case statement to do nothing for those values. break means to break out of the switch statement entirely. Your switch statement does not perform any operations for the values 2, 3, 4, 5, 7, and 8.

I also believe that you need to look more carefully at what you are supposed to return.

Having seen a few of your in-progress solutions, Iā€™d like to offer some general advice when it comes to your approach:

  1. Read the instructions very carefully. Read the instructions several times. Break them down and make sure you know exactly what the expected results should be.
  2. Work on your solutions in small steps. Start with the most basic requirements or the simplest case from the instructions and focus on getting that right. Then build off of that.
1 Like

I feel incredibly bad for asking when I do, but sometimes I donā€™t understand the questions. This has occured less over time, but especially when I was learning what a switch was (how I should think about it so that I respond to situations where a switch would be useful correctly, I mean), I had trouble making an action plan and responding to a novel situation. Would you suggest writing a ā€œto doā€ list:

I am asked I have done
x x
y y
z z

?

The formatting is horrible here, but I hope you get what I mean. two columns essentially.

1 Like

I have actually solved some of the questions while I was critically thinking about it before a moderator has given me advice about what to do. But I will keep your advice in mind.

counting cards Nov 30

Example of my critical thinking.

Never feel bad about asking! I certainly wasnā€™t trying to shame you for that.

1 Like

Look at my code^)

var count = 0;
function cc(card) {
if (card>1 && card<7)
{count++;}
else if (card>6 && card<10)
{count=count;}
else if (card == 10 || card == ā€˜Jā€™ || card == ā€˜Qā€™ || card == ā€˜Kā€™ || card == ā€˜Aā€™)
{countā€“;}
if (count>0)
{return count + " Bet"; }
else if (count<=0)
{return count + " Hold";}
else {
return ā€œChange Meā€;}

2 Likes

hi owen, how much different is my code from yours:

function cc(card) {
  // Only change code below this line
    if(card>=2 &&card<=6){
        count++;
        return count + "Bet";
      }
    else if(card>=7 && card <=9){
        return count + "Hold";
      }
    else if(card==10||card=='J'||card=='Q'||card=='K'||card=='A'){
        count--;
        return count + "Hold"; 
      }
  else return "Change Me";
  // Only change code above this line
}

my code doesnā€™t run at allā€¦

Wow, thanks a lot randel, I really appreciate it.

hi randel, sorry I am back again but I am still not passingā€¦not sure what I am still doing wrong, my code below:

  // Only change code below this line
    if(card >= 2 && card <= 6)
        {count++;}
   else if(card >= 7 && card <= 9)
      {count = count;}
        
    else if (card==10 || card=='J' || card=='Q' || card=='K' || card=='A')
        {count--;} 

    if (count > 0)
    {return count + " Bet";}

    else if (count <= 0);
    {return count + " Hold";}

  else return "Change Me";
  // Only change code above this line
}'''

omg the rogue semi-colon was all that was stopping the program from doing what I expected it to doā€¦

1 Like

Hello and welcome to the FCC community~!

Did you have a question? Was there something we could help you with? In general, when you have a question it is best to create your own forum topic, instead of adding your question to an older topic. :slight_smile: