Couting Cards javaScript

Hi Freecode Campers !

I’ve been stuck here since several days and I don’t find any solutions working…

Anyone keen to help me through this exercise please ?

Cheers, Vincenzo.

2 Likes

You could try a switch statement that falls through or a if else statement with /regex/i.test(str) to test for true.

I had done this one a while ago, and I couldn’t find my code, so I redid it with a switch, which works fine.

It’s difficult to give much more advice without giving the whole answer away 8-). Why don’t you post something you tried, maybe the version you would most like to get working, and we can help once we see it.

What have you tried so far?

I’d say the if else is the easiest way to go, and you don’t even need a regex–just a typeof. But I do agree with the others that it’s easier to help if we can see what @Zigantheshaman has been thinking and tried thus far.

I will leave the implementation to you ( :wink: ) but you can use a switch to check the card and increment/decrement the count value, then a if to return the correct message depending on the count value…

1 Like

I’m stuck on the Count Cards problem, too. I have two questions:

  1. I’m having trouble figuring out how to increment value of count. I’m pretty sure I need to declare another var somewhere to hold the inner count but I’m not sure where. If my hunch is right, will someone give me a hint?

  2. Should the switch statement and if statement be as lexically separated as I have made them?

`
var count = 0;

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

switch(card){
case (card >= 2 && card <= 7):
// console.log (“count > 1 && card < 7”, count);
return counter += 1;

case (card >=7 && card <=9):
  // console.log ("count >=7 && card <=9", count);
  return counter += 0;
  
case (card >= 10 || (typeof card === 'string')):
  // console.log ("count >= 10", count);
  return counter -= 1;  

}

if (count > 0) {
return count + " Bet";
}

else{
return count + " Hold";
}
// Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); //cc(3); cc(7); cc(‘K’); cc(‘A’);
`

This is what I’ve been trying so far

Oh, you are soo close. and it’s probably just an oversight.

One question should probably put you right:
What should the function do if count is equal to 0?

As a side note. to make it easier for people to help you, it’s usually better to copy/paste your code here (just put a backtick (`) before and after your code block to format it right) I had to retype your whole thing since I couldn’t copy/paste a picture (granted, had I looked it over first, I probably would have caught the error without needing to do that, but often I find it’s easier to help people by trying out their code and playing with it.)

`var count = 0;

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

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

The mistake was in my else if parameter :slight_smile:

exactly. :slight_smile: You could have also made it else if (count <= 0)... but what you did works too.

//the solution to correct my error in the below (that my count was always returning 0) was to set the switch param to a boolean value
`
var count = 0;

function cc( card ) {
// Only change code below this line
var test = " ";
switch( card ){
case ( card >= 2 && card <= 6 ):
count += 1;
break;
case ( card >=7 && card <=9 ):
count += 0;
break;
case ( card >= 10 || ( typeof card === ‘string’ ) ):
count -= 1;
break;
}

if (count > 0) {
return count + " Bet";
}
else {
return count + " Hold";
}
// Only change code above this line
}
// Add/remove calls to test your function.
// Note: Only the last will display ~test one card value at a time
cc(10); cc(‘A’); cc (‘Q’); /cc(‘K’); cc(‘A’)/;
console.log( cc(‘K’) ); //logged -4 Hold
`

1 Like

Hi there!

I’m new here and I’m stuck on this Counting cards challenge too. The code I’ve got is similar to Zigantheshaman but I haven’t included the var test = ""; variable and I don’t understand what it does (I think it must hold the count value(?) but I don’t know why!) And I also don’t understand why my code doesn’t work without it…! To clarify, my code is as follows:
`var 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;
case = 7:
case = 8:
case = 9:
count += 0;
break;
case = 10:
case = “J”:
case = “Q”:
case = “K”:
case = “A”:
count --;
break;

}

if (count > 0) {
return count + " Bet";

} else if
(count <= 0) {
return count + " Hold";
// Only change code above this line
}
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(4); cc(5); cc(6);
`
And I’m getting about 5 errors on the first line under ‘switch’. (Presumably because I’ve left out the ‘test’ variable).
Can anyone help? Thank you in advance!!

dohhhhh! Well, I just realised I’d put ‘=’ after each ‘case’ and there shouldn’t be one (didn’t even realise despite one of the error messages saying there was an unexpected = in the first case line - I guess I’ve been staring at the screen for too long!). And now the challenge is passing even without the test variable so I’m happy - but I’d still like to know why you used the test variable? Thank you!

I’m not going mad!!! Good to know - thank you! :slight_smile:

1 Like

Thanks for this! I read through this because I got stuck and realized I didn’t capitalize Hold and Bet haha.

just solved my problem too. found out i should use “+” instead of a comma when return a value…wow…this is so tricky…almost freaked out

After writing the code several different ways, realized I had Bet and Hold returning under the wrong conditions. Since the output was “0 Bet”, in my case, I got locked into thinking the problem was the count not changing. Finally noticed the count was supposed to be 0 and, after changing to the first test case, the code was counting fine and betting to lose!