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)?

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)?
0

#1

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.


#2

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.


#3
 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.

#4

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

?


#5

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


#6

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.


#7

counting cards Nov 30

Example of my critical thinking.


#8

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


#9

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”;}


#10

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
}

#11

my code doesn’t run at all…


#12

I’ve edited your post for readability. When you enter a code block into the forum, 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.


#13

Your code runs, but it returns the wrong answer in all the test cases.

The biggest problem you have is that you need to separate the logic which increases and decreases the value of count from the logic which actually returns the applicable string.

Currently, you are correctly increasing and decreasing the count variable based on the card values, but then your return statements are inside the same if statements concerning the card values.

Note: The card values in any given call to the function do not directly determine what gets returned. Only the current value of count determines what gets returned.

Lastly, make sure the string you return includes the count plus a space character and then the word Hold or Bet. Currently you have no space between the count value and the string “Hold” or “Bet” which will causes test failures.


#14

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


#15

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
}'''

#16

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