Basic js: counting cards

when I run this code, it prints out every operation it does of adding or subtracting 1. The problem only asks for the end operation. How do I fix this?
Your code so far


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 += 1
break;
case 7:
case 8:
case 9:
count += 0
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count += -1
break;
}
if (count >= 1){
console.log(count + ' bet') 
}else if(count <= 0){
console.log(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/83.0.4103.106 Safari/537.36.

Challenge: Counting Cards

Link to the challenge:

Hello
You have to return the output and you are only printing it to the console.

1 Like

Are you saying I need to use the return command? Also, aren’t I already printing it out to the console?

return and console.log are very different. You need to use return. (in general you want return).

I replaced console.log for return in the if statements but when I run it in vscode there isn’t an output.

Also I did the same for my code in the website and some came out right but the ones that came out as a positive number were wrong.

Please share your updated code.

There is a typo in this string.

htyhey

Screenshots are very difficult to work with. Please copy and paste your code into a post.

That said, I think you still have that typo

return(count + ' bet')
var count = 0;

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

  // Only change code above this line
cc(2); cc(3); cc(7); cc('K'); cc('A');

My apologies! I don’t know how to add code into a reply.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

Please use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.

markdown_Forums

1 Like

I’m not sure why you changed the switch. The switch in your first post is more readable.

  return(count + ' bet') 

You have the wrong capitalization.

Unrelated, but still worth fixing:

if (a >= thisNumber) {
  ...
} else if (b <= thisNumber - 1) {
  ...
}

In this case it doesn’t make a lot of sense to have an else if. Why not use an else?

var count = 0;

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

}

  
  // Only change code above this line


cc(2); cc(3); cc(7); cc('K'); cc('A');

That was my edited code after realizing that was faster. The code didn’t save and I was too lazy to retype it :sweat_smile: . What do you mean by capitalization? Also, yeah that does make more sense I will make sure to change that.

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 current count and the player’s decision ( Bet or Hold ) should be separated by a single space.

Do you have Bet?

1 Like

OHHHHHHH thank you!!!

1 Like

Happy to help. Those little things trip us all up.

1 Like

umm also I replaced the else if with else and it gave me a red underline under the bracket that conatains the return statements.