Using IF statement rather than SWITCH statement

Tell us what’s happening:
Describe your issue in detail here.
Hey, I’m struggling with this problem. I have seen the switch statement solution and can understand most of it. however when I try solving it with an if statement, i get error. see what I’m doing wrong. Thanks.

  **Your code so far**

var count = 0;

function cc(card) {
// Only change code below this line

if(card >= 2 && card <= 6){
return count + ' ' + 'Bet';
}else if(card >=7 && card <= 9){
return count + ' '+ 'Hold';
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/91.0.4472.124 Safari/537.36

Challenge: Counting Cards

Link to the challenge: text

I am getting the error:

ReferenceError: count is not defined

That is because count is undefined. You need to declare and initialize it.

Also, I would be a little worried about something like card >= 2 - what does that mean if the card is a string? I don’t know, I can’t test it right now, but that seems odd to me to be comparing it this way with no accounting for the string values.

Screen Shot 2021-07-09 at 8.23.48 AM

Additionally, there’s also some logical error. Imagine you have the sequence

cc(2); cc(2); cc(2); cc(2); cc('A')

After the first four function calls, the count should be 4. Then you get an A, which would reduce the count to 3, and the function should return 3 Bet, but yours returns 3 Hold. You’d have to check the value of count before you decide whether you want to return Bet or Hold.

Yes, doing this with if/else is definitely doable. The code is going to be messy.

I think that issue of the comparing numbers and strings is a big problem. One way to handle it would be to set and internal variable for the card, and if it is a string, assign it a value of 10 since they all count the same.

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