Counting Cards: I'm stuck and would like guidance for where I'm deficient

Counting Cards: I'm stuck and would like guidance for where I'm deficient
0

#1

Tell us what’s happening:

I’ve read through several of the posts on this particular puzzle and I’m just not getting it. If you would please look at my code below and tell me what I’m overlooking because I’m sure I am but I can’t push any farther. Also, I’ve tried switch to solve it, but I’ve come to the same issue. I’m stuck. A guiding/gesturing/correcting hand in the area of both possible syntax for solving this problem would be greatly appreciated.

Thank you

Your code so far


var count = 0;

function cc(card) {
  // Only change code below this line
  if (card <= 6) {
    count++;
  }
  else (card >= 10); {
    count--;
  }

if (card = count++) {
  return "Bet";
} else {
    return "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');

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/counting-cards


Counting Cards Switch Syntax Query
#2

You’re very close, but you’ve missed a slight detail in the instructions, as well as something important about the high value cards

Firstly:

The function will then return a string with the current count and the string Bet if the count is positive, or Hold if the count is zero or negative.

The above has nothing to do with the current card value, also not sure why you’ve incremented it there either

Secondly, the high value cards aren’t passed as numbers, they’re passed as strings, e.g. K for king (which you assumed would be 13)


#3

Okay. I feel as if this might be even worse, but I’ve attempted to follow what I think you were hinting at in terms of the “high value cards”. I have attempted to account for the dealing of 5 cards of low suit (card <=6) and (count+5) as indicating a positive integer and, therefore, that the player should “Bet”. And vice-versa for the dealing of 5 high cards (card >9) and (count -5) that indicate a “Hold”.

However, it feels as if I’m not addressing a deadly important issue or aspect of the function. I’m not sure if this is [1]to do with my syntax or if [2] I’m neglecting to include an element or item in my function.

var count = 0;
function cc(card) {
if (card <=6)  {
(count + 5) = 1;
} else (card >9) {
(count-5)  = -1;
}

if (card+5) {
return "Bet";
} else {
return "Hold";
}


return "Change me";


#4

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#5

Tell us what’s happening:
I created a post trying to understand how to solve counting cards with if/else syntax (haven’t managed yet) and I wanted to ask for advice in trying to solve this problem in switch syntax. Would someone mind telling me if I am on the right track at all?

Thanks.

Your code so far


var count = 0;

function cc(card) {
  switch(card) {
    case 2: 
    case 3: 
    case 4: 
    case 5: 
    case 6:
    count += 1;
    break;
    case 10:
    case "J":
    case "Q":
    case "K":
    case "A":
    count -= 1;
    break;
  }

  if (card <=6) {
    card<=6 + 5;
    return "Bet";
  } else {
    return "Hold";
  }
  return "Change me";
}
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/64.0.3282.140 Safari/537.36 Edge/17.17134.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/counting-cards


#6

I will address the post using the switch statement. It works perfectly for calculating the correct value of count. The problem is your logic for determining what string to return and the actual string returned.

What gets returned from your function is only indirectly related the card values. What really determines the returned string value is what the value of count is. If count is greater than zero, then you want return a string which is a concatenation of the value of count with " Bet". Otherwise, the returned string will be a concatenation of the value of counter with " Hold".


#7

I altered the end of the switch sequence:

if (cc(card = count > 0)) {
return “Bet”;
} else {
return “Hold”;
}

The cc function includes the cases of all the cards which still allow for a return of “Bet”. This doesn’t past the test, so I’m still missing something though.


#8

You do not need to call the cc function again inside the if statement. You just need to compare count to zero. Also, make sure you are returning the count value concatenated with " Bet" or " Hold" and not just “Bet” or “Hold” which your current code is doing.


#9

Is this what you were referring to in regard to concatenation?

if(card > count) { return "Bet"+count; } else { return "Hold"-count;

#10

Close. Look at the instructions and make sure you have the order of the concatenation and the correct spacing. Also, - is not the correct operator for concatenation. Instead, use +.


#11

Is this what you were referring too?

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


#12

You are 99.99% there. You still do not have the space needed. Your return statement will return something like

"5Bet"

instead of

"5 Bet"


#13

Okay. I understand.
if(count>0){return count + “Bet”;}

Although FCC is still not passing me. It says “//running test”, which I assume may have something to do with connection speed on my part because I believe I have passed.

Thanks @randelldawson


#14

Post your full code again so we can take a look at what you have. More than likely you have a syntax issue which would show up as an error in your browser’s console (Ctrl+Shft+J in Chrome) when trying to run the tests.

Also, make sure you are using straight double or single quotes instead of those curly ones I see in your last post. Those are not valid string identifiers in JavaScript which would cause an error.


#15

Tell us what’s happening:

Your code so far


var count = 0;

function cc(card){
 switch(card){
   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
   count +=1;
   break;
   case 10:
   case 'J':
   case 'Q':
   case 'K':
   case 'A':
   count -=1;
   break;
 }
 if(count>0){
   return count + 'Bet';
 }
 }
  cc(2);

#16

Your latest attempt still does not address the spacing issue I mentioned in my last reply for returning the count and " Bet". Also, you are not returning the count and " Hold" for when count is less than or equal to zero.


#17

This is what I have so far. It’s not testing out, but I’m not sure why. Logically, it seems that the return would generate the appropriate data and I believe I have addressed the spacing problem, but perhaps I misunderstood what you were hinting at.

What am I missing?

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

#18

As I explained above regarding posting code in a forum post, you must use 3 backticks on the line before the code and 3 backticks on the line after the code you post. I have edited your last post to properly format, but in the future, please try to do this yourself.

As far as your code goes, your code will still return strings without the required space character between count and the words “Bet” or “Hold”.

For example, if count is equal to 5, your code will return:

"5Bet"

instead of the correct value of:

"5 Bet"

See how the second string above has a space character between 5 and Bet?