Can’t solve counting cards

So I’m really confused by this one. Apparently I have to set the value of the cards so the “Bet 6” or whatever string is returned but I can’t seem to do that without it throwing up errors. When I try to use switch that doesn’t work either. Unless I’m supposed to write out every single card but that seems really impractical.

Can anyone help me work out what I’m supposed to be doing here?

(Please, please don’t give me answers I’m trying to work this out. Not have it solved for me.)

let count = 0;

function cc(card) {
// Only change code below this line
var ["2","3","4","5","6"] = +1;
var ["7","8","9"] = 0;
var ["10", "J","Q","K","A"] =  -1;

switch (card){
case 2,3,4,5,6:
return "Bet";
break;
case 7,8,9:
return "Hold";
break;
case 10,"J", "Q","K","A":
return "Hold";
break;
}
return "Change Me";
// Only change code above this line
}

cc(2); cc(3); cc(7); cc('K'); cc('A');
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15

Challenge: Counting Cards

Link to the challenge:

Your code contains a lot of syntax errors.

I’d suggest you to try to solve it using the most straightforward approach, just to make it work without any optimizations first.

1 Like

Hi @EllaGriff !

I would suggest resetting the lesson.

This challenge has two main components.

The first component is checking what the card is and deciding if we need to increment the count or decrement the count.

The second component is to check if the count is positive then return the count and Bet otherwise return the count and Hold.

Try to focus on the first component and figure out how you are going to increment or decrement the count based on the card.

As a side note, if you decide to use a switch statement, then you should read up on this article to help you with syntax.

Hope that helps!

1 Like

I agree with this too.

Don’t worry about coming up with the cleanest solution right now.
Just focus on solving the problem even if it is a little messy.
Then once you solve it, you can go back and refactor it.
Or you can share your solution in the forum wrapped in spoiler tags [spoiler] and ask the forum for help on optimizing your code.

This will come in handy when you start working through the basic and intermediate algorithms.

Hope that helps!

2 Likes

I have this now. I’m trying to use switch to check the card and increase and decrease the count. Not sure if I’m on the right track. I’m trying to use the if/else to put together the string it wants but that won’t work for some reason.

let count = 0;

function cc(card) {
  // Only change code below this line
switch (count){
  case 2:
  return +1;
  break;
  case 3: 
  return +1;
  break;
  case 4:
  return +1;
  break;
  case 5:
  return +1;
  break;
  case 6:
  return +1;
  break;
  case 7:
  return 0;
  break;
  case 8:
  return 0;
  break;
  case 9:
  return 0;
  break;
  case 10:
  return -1;
  break;
  case "J":
  return -1;
  break;
  case "Q":
  return -1;
  break;
  case "K":
  return -1;
  break;
  case "A":
  return -1;
  break;
}
if (card > 0) {
return "Bet" + count;
}
else if {
  return "Hold" + count;
}
else {
  return "Change Me";

  // Only change code above this line
}

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

you need to switch a card value, not a count

and here the count var should be incremented or decremented. You don’t want to return any values from switch.

resulting string should contain a blank between the word and the count

here condition is missing

also } is missing. function’s body isn’t closed

1 Like

The first thing you want to fix is this part

You don’t want to return 1,0, or -1

The first part of the challenge wants you to increment the count variable or decrement it based on the card.

There was a fcc challenge where you learned how to increment and decrement variables.
I would review these two lessons

Then you can move onto the other issues that were mentioned.

Hope that helps!

1 Like

Ah I think given I’ve forgtton those lessons entirely I’m going to also go though the whole course again to this point. Just to try to get things to stick more.

Thanks for explaining thought!

1 Like

Okay now I have this. It still doesn’t work. I’m really really unsure where to go from this.

let count = 0;

function cc(card) {
// Only change code below this line
switch(card){
case 1:
count++;
break;
case 2:
count++;
break;
case 3:
count++;
break;
case 4:
count++;
break;
case 5:
count++;
break;
case 6:
count++;
break;
case 7:
count +0;
break;
case 8:
count +0;
break;
case 9:
count +0;
break;
case 10:
count--;
break;
case "J":
count--;
break;
case "Q":
count--;
break;
case "K":
count--;
break;
case "A":
count--;
break;
}
if (count >0){
return "Bet " + count;
}
else if (count ==0){
return "Hold " + count;
}
else if (count < 0){
return "Hold " + count;
}
else{
return "Change Me";
// Only change code above this line
}
}
cc(2); cc(3); cc(7); cc('K'); cc('A');

The only semantic mistake is:
Example Outputs: -3 Hold or 5 Bet // first count goes and then the word

Also, a few notes about optimization:

let count = 0;

function cc(card) {
  
  switch (card) {
    // switch has so-called "fall through" functionality
    // so if you want to execute the same block of code under a few 
    // conditions you may write like:
    // switch (val) {
    //   case 1:
    //   case 2:
    //   case 3:
    //     doSmething();
    //     break;
    //   case 4:
    //   case anything:
    //     do();
    //     break;
    // }
    case 1:
      count++;
      break;
    case 2:
      count++;
      break;
    case 3:
      count++;
      break;
    case 4:
      count++;
      break;
    case 5:
      count++;
      break;
    case 6:
      count++;
      break;
    // check for 7, 8, 9 is unnecessary as we do nothing with count 
    case 7:
      count + 0;
      break;
    case 8:
      count + 0;
      break;
    case 9:
      count + 0;
      break;
    case 10:
      count--;
      break;
    case "J":
      count--;
      break;
    case "Q":
      count--;
      break;
    case "K":
      count--;
      break;
    case "A":
      count--;
      break;
  }

  // you may simplify this if/else if/else if/else statement
  if (count > 0) {
    return count + " Bet";
  }
  // the following conditions are redundant, because we already know
  // the count here will be less than or equal to zero because we have checked it already
  // in the first if (count > 0)
  else if (count == 0) {
    return count + " Hold";
  }
  else if (count < 0) {
    return count + " Hold";
  }
  else {
    return "Change Me";
    // Only change code above this line
  }
}
cc(2); cc(3); cc(7); cc('K'); cc('A');

Hi I’ve got this now.

let count = 0;

function cc(card) {
// Only change code below this line
switch(card){
case 1:
count++;
break;
case 2:
count++;
break;
case 3:
count++;
break;
case 4:
count++;
break;
case 5:
count++;
break;
case 6:
count++;
break;
case 10:
count--;
break;
case "J":
count--;
break;
case "Q":
count--;
break;
case "K":
count--;
break;
case "A":
count--;
break;
}
if (count >0){
return count + "Bet ";
}
else if (count <0){
return count + "Hold ";
}
else{
return "Change Me";
}
// Only change code above this line
}

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

And it still doesn’t work. Is there somewhere I’m going wrong I’m majorly missing?

Hi, just fix the blank. Your current output looks like "-5Bet " instead of "-5 Bet"

I’ve fixed that thanks for telling me. :smiley:

But I’m now getting this error,

// running tests Cards Sequence 7, 8, 9 should return the string

0 Hold

// tests completed

let count = 0;

function cc(card) {
// Only change code below this line
switch(card){
case 1:
count++;
break;
case 2:
count++;
break;
case 3:
count++;
break;
case 4:
count++;
break;
case 5:
count++;
break;
case 6:
count++;
break;
case 10:
count--;
break;
case "J":
count--;
break;
case "Q":
count--;
break;
case "K":
count--;
break;
case "A":
count--;
break;
}
if (count >0){
return count + " Bet";
}
else if (count <0){
return count + " Hold";
}
else{
return "Change Me";
}
// Only change code above this line
}

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

less than or equals to <=
or you may use just else here without any conditions like

if (a > 0) {
  //
} else {
  // if we are here then we know that **a**  is not greater than 0, hence it is less than or equals to 0
}

Ah thanks I’ve got it passed! I think I’m going to have to go over greater than and less than in Javascript again…

1 Like

By the way instead of applying things to every single case, you can group them together like this if multiple cases do the same exact thing:

switch (card) {
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
    count++;
    break;
  case 10:
  case "J":
  case "Q":
  case "K":
  case "A":
    count--
    break;
}

The above cuts down on a lot of code.

1 Like