Counting Cards - Issue w/ "1 Bet" function in Switch Statement

Counting Cards - Issue w/ "1 Bet" function in Switch Statement
0

#1

Tell us what’s happening:

Something odd is happening during two of the FCC tests :slight_smile:

  • Cards Sequence 2, J, 9, 2, 7 should return “1 Bet”
  • Cards Sequence 2, 2, 10 should return “1 Bet”

CONSOLE LOG —

My Console Log shows the correct outputs but for some reason FCC marks this as wrong.

Screenshot:

TEST ONE - Cards Sequence 2, J, 9, 2, 7

card given: 2. Current count is 0. Incrementing count. New count 1 VM1368:16
1 Bet VM1368:36

card given: J. Current count is 1. Reducing count by 1. New count 0 VM1368:39
0 Hold VM1368:23

card given: 9. Current count is 0. Do Not Increment or Reduce count. Count is 0 VM1368:26
0 Hold VM1368:14

card given: 2. Current count is 0. Incrementing count. New count 1 VM1368:16
1 Bet VM1368:23

card given: 7. Current count is 1. Do Not Increment or Reduce count. Count is 1 VM1368:14


TEST TWO - Cards Sequence 2, 2, 10 should return “1 Bet”

card given: 2. Current count is 0. Incrementing count. New count 1 VM1368:16
1 Bet VM1368:14

card given: 2. Current count is 1. Incrementing count. New count 2 VM1368:16
2 Bet VM1368:36

Card given: 10. Current count is 2. Reducing count by 1. New count 1


I have a feeling is a bug or a simple typo but i cant seem to find it. Any help or insight is appreciated. Thank you.

Your code so far

var count = 0;

function cc(card) {
  // Only change code below this line
  
  switch (card) {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      console.log("card given: " + card + ". Current count is " + count++ + ". Incrementing count. New count " + count);
      if (count >= 0) {
        console.log(count + " Bet");
        return count + " Bet";
      } 
      break;
    case 7:
    case 8:
    case 9:
      console.log("card given: " + card + ". Current count is " + count + ". Do Not Increment or Reduce count. Count is " + count);
      
      if (count == 0) {
        console.log(count + " Hold");
        return count + " Hold";
        
      }
      break;
    case 10:
    case "J":
    case "Q":
    case "K":
    case "A":
      console.log("card given: " + card + ". Current count is " + count-- + ".  Reducing count by 1. New count " + count);
    
      if (count <= 0) {
        console.log(count + " Hold");
        return count + " Hold";
      }
      break;
  }
  
  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc('J'); cc(9); cc(2); cc(7);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:


#2

It might be your use of a global variable. See the comment here: JavaScript and Vars


#3

One problem with you code involves cases 7, 8, or 9.

    case 7:
    case 8:
    case 9:
      console.log("card given: " + card + ". Current count is " + count + ". Do Not Increment or Reduce count. Count is " + count);
      
      if (count == 0) {
        console.log(count + " Hold");
        return count + " Hold";
        
      }
      break;

What if card is a 7,8, or 9 and count is 5? Your code just stops (break statement), but does not return anything. You have similar logic issues with the other cases, but I bring this one up, because it directly affects the one specific test case you are failing.

What if a card is a 2, 3, 4, 5, or 6 and count is less than 0?

What is a card is a 10, "J’, ‘Q’, ‘K’, or ‘A’ and count is greater than 0?


#4

Thank you for clarifying this. I see the logic error now.