Basic JavaScript - Counting Cards Difference between ++ and + 1

Hello, so as you can see in the code included I got the solution to this problem by taking care of multiple inputs and using the case++; method at the end of case 6.

But what I am confused at is I initially tried this:

case 2:
count + 1;
case 3:
count + 1;
case 4:
count + 1;
case 5:
count + 1;
case 6:
count + 1;
break;

and more annoyingly this

case 2:
case 3:
case 4:
case 5:
case 6:
count + 1;
break;

and neither worked, until i merely just had count++; at the end of case 6. Can somebody please explain to me the difference here ? Many thanks and much appreciated.

As you can see below is my answer that worked.

let count = 0;

function cc(card) {
  // Only change code below this line
  switch(card){
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      count ++;
      break;

  }

  if (count > 0){
    return count + " Bet";
  }
  else{
    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/115.0.0.0 Safari/537.36 Edg/115.0.1901.203

Challenge: Basic JavaScript - Counting Cards

Link to the challenge:

Welcome to our community!

count++ means count = count + 1.

If you pass the value 2 to the function it should return 1 Bet, because the count variable now has the value count = 0 + 2 and that is greater then 0:

 if (count > 0) {  ....here, the count is still 0 if you use `count + 1` after each case
    holdbet = 'Bet'
  }

In the case you use count + 1, it doesn’t do anything in the code. The ‘count’ variable still holds the value 0, because you didn’t assign the new value to it. You will get 0 Hold 0 Hold 0 Hold... for all cases (2, 3, 4, 5, 6).

Thank you so much, I really appreciate it. So I see now where my problem was, even simply stating count = count + 1 would have solved my problem as count + 1 does not copy the value to count. Thank you again and its great to find somewhere to discuss.

1 Like

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