Counting cards task, not sure on why my solution is wrong

Tell us what’s happening:
Describe your issue in detail here.
Using the console.log() function for the test example in the code I get this back:

0 Bet
0 Bet
0 Bet
-1 Bet
-2 Bet

Which indicates to me that my negative increments are working, but the positive ones are not for some reason. Additionally if I add counts++ to some of the cases, sometimes tests will pass and sometimes they won’t, but never all of them, and I’m just completely clueless on what is going on. Any help would be much appreciated, thanks!
Your code so far


let count = 0;

function cc(card) {
// Only change code below this line
switch (card) {
case card = 2:  
case card = 3:  
case card = 4:  
case card = 5:  
case card = 6:
  count++;
case card = 10:
case card = 'J':
case card = 'Q':
case card = 'K':
case card = 'A':
  count--;
}

if (count > 0 ) {
return count + ' Bet'
}

console.log(count + " Bet")
return count + ' Hold'
// 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Counting Cards

Link to the challenge:

This isn’t a good case statement. This says "assign the value 2 to the variable card and check if this matches the variable card".

The case should only be the value that you want to match. Nothing more or less.

I was under the impression that cases always check for strict equalities, is this wrong? I just replaced them with strict equalities in the code and all lines came out as 0 bet. Also if I was reassigning values, I’m positive I would always get 0 as it would be adding 1 and taking away 1 each time.

The case checks will strick equality without you needing to use the strict equality operator.

switch (card) {
  case card = 2:
//...
}

is the same as

if (card === (card = 2)) {
// ...
}

Ok i understood what you meant now and changed the code to the following:

function cc(card) {

  // Only change code below this line

switch (card) {

  case 2:  

  case 3:  

  case 4:  

  case 5:  

  case 6:

    count++;

  case 10:

  case 'J':

  case 'Q':

  case 'K':

  case 'A':

    count--;

  }

However the console still shows :

0 Bet
0 Bet
0 Bet
-1 Bet
-2 Bet

for the practice:
cc(2); cc(3); cc(7); cc(‘K’); cc(‘A’);

Woops, don’t forget to break

1 Like

Oh, that was a silly mistake! Thank you very much.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.