Counting Cards " 'J' is not defined" HELP!

<%= @topic_view.topic.title %>
<%= @topic_view.topic.average_rating %> <%= @topic_view.topic.posts.count { |p| !!p.custom_fields['rating'] } %>

Tell us what’s happening:
This code is failing the test with: " ‘J’ is undefined"
What am I missing?

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 ++;
    break;
    case 7:
    case 8:
    case 9:
    count = count;
    break;
    case 10:
    case J:
    case Q:
    case K:
    case A:
    count --;
    break;
  }
  if (count <= 0){
    return count + "Hold";
  }
  else {
    return count + "Bet";
  }
  
  // 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; rv:68.0) Gecko/20100101 Firefox/68.0.

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

sorry, the exact message is, " ‘J’ is not defined.", not “undefined”.

If you want to test for the string J, then you must surround it with either double quotes or single quotes. Otherwise, JavaScript thinks it is a variable which you have not defined anywhere.

1 Like

I am only getting this error with regards to J not having an issue with the other letters. When I add quotes, it fails all over the place.

You are going to need to put quotes around the other letters also or you will have the same issue. Make those changes and if you are still failing the tests, post your latest code again.

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 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.

Note: Backticks are not single quotes.

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 = count;
    break;
    case 10:
    case "J":
    case "Q":
    case "K":
    case "A":
    count --;
    break;
  }
  if (count <= 0){
    return count + "Hold";
  }
  else {
    return count + "Bet";
  }
  

  // Only change code above this line
}

fail.

That is because you are returning values like: “4Bet” and “-2Hold” instead of “4 Bet” and “-2 Hold”. Look carefully at the last line in the instructions.

Got it. Thanks! I knew it was going to be a detail like that. :slight_smile: