Basic JavaScript: Counting Cards Help?

Tell us what’s happening:

Not sure what to do with these since i’m already using some of these values? Like, maybe switch statement wasn’t the best choice here because i can’t use || like inside a if statements parentheses?

Cards Sequence 2, J, 9, 2, 7 should return 1 Bet

Cards Sequence 2, 2, 10 should return 1 Bet

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++;
  return count + " Bet";
  break;
  case 7:
  case 8:
  case 9:
  return count + " Hold"
  break;
  case 10:
  case "J":
  case "Q":
  case "K":
  case "A":
  count--;
  return count + " Hold";
  break;
}

// 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 6.2; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0.

Challenge: Counting Cards

Link to the challenge:

your switch case should at the end have an if statement. if the given number is more than 0 return “Bet” otherwise return “hold”.

if (X > 0) {
    return "this";
  } else {
    return "that";
  }

that doesn’t do anything

if (card > 0) {
    return " Bet";
  } else {
    return " Hold";
  }

you need to modify count as well.
you current solution is

return count + "Bet";

and

return count + "Hold"

( here you don’t have a semicolon)
remove these two parts. and add the if statement. It should work

I don’t understand how the below would fix the 2 remaining sequence? because 0 is an int right and j is a string? which in the image below J isn’t an int so not sure how count > 0 knows j is a string?

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

you need to change count based on card

You will write a card counting function. It will receive a card parameter, which can be a number or a string, and increment or decrement the global count variable according to the card’s value (see table).

And then return a value based on count

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.

If you have made changes and still need help please post your new code

I just looked at the solution because would have never thought of the code below without looking at the solution. I also still don’t understand how count can know about J …anyways thanks all

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

read this:

Not a real dictionary, but Unicode order

The comparison algorithm given above is roughly equivalent to the one used in dictionaries or phone books, but it’s not exactly the same.

For instance, case matters. A capital letter "A" is not equal to the lowercase "a" . Which one is greater? The lowercase "a" . Why? Because the lowercase character has a greater index in the internal encoding table JavaScript uses (Unicode). We’ll get back to specific details and consequences of this in the chapter Strings.

from this resource https://javascript.info/comparison

I think you are missing a few things

J doesn’t know about count

you change count based on the value of card:
if the card is 2,3,4,5,6 count is increased by one
if the card is 7,8,9 count is not changed
if the card is 10,J,Q,K,A count is decreased by one

then the valye of count is checked, and based on that the output is determined

So your saying i associated strings with an int? so anything of negative value = 10,J,Q,K,A regardless of data type?

can you indicate what part of the code is giving you issues? I do not understand your question

I don’t understand how the below would fix the 2 remaining sequence? because 0 is an int right and j is a string? which in the image below J isn’t an int so not sure how count > 0 knows j is a string?

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

cc(2) : 2 is in the first group of cards, so count is increased by one (count is 1)

cc(J) : J is in the third group of cards, so count is decreased by 1 (count is 0)

cc(9) : 9 is in the second group of cards, so count is not changed (count is 0)

cc(2) : 2 is in the first group of cards, so count is increased by 1 (count is 1)

cc(7) : 7 is in the second group of cards, so count is not changed (count is 1)

now, the Bet/Hold part is decided by the value of count

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.

more specifically:

a string with the current count and the string Bet if the count is positive

so you need to return “1 Bet” in this case, because count is positive


instead you are returning “1 Hold” beacuase of this return statement:

I already looked at the solution and copied it i don’t need help solving this i need you to explain to me how count 0 knows of “j” which is a string not an int.

You stated earlier;

and i replied to the above with a yes or no answer?

So yes or no?

“J” is checked here, and there is written case "J" so it’s checking for a string, as that’s what’s written there

count doesn’t know anything, you are determining how it should be changed

Then how does this line of code fix the image below

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

everything makes sense because 2, 9,2,7, are numbers so count < 0 is understandable but J isn’t an int so how does this code work on j.

becase your issue is that you are returning values from inside the switch
that means that you are not checking what’s the value of count before returning a value

you are returning Hold just because the card is a 7, not because you are checking what’s the value of count

instead you need to have two separate parts of the function, one changing count, and one determining the output of the function based on the value of count

So your saying j is associated as an int? so anything of negative value = 10,J,Q,K,A regardless of data type?

So if count is -1 that is = to J?

Yes or No?

"J" is not associated with an int, "J" is a string, and if card has that value then there is a specific action to be done

if the value of card is one of 10, "J", "Q", "K" or "A" then count must be decreased as instructed

if count is 10, then the next card is "Q", count is decreased and becomes 9


you need to check the value of count to know if you need to return "Bet" or "Hold", "Bet" is for positive values, "Hold" for 0 or negative values

I’m done here we are not getting anywhere…you said " "J" is not associated with an int, "J" is a string" but then you said " if count is 10 , then the next card is `“Q”.

So if count is -1 that equals all the Hold Cases that are negative and “J” is a case which is a string !!! so the count int is grabbing the string J and its associated which = Yes to the question i asked 3 times…

Update - I can’t reply anymore so read here for now on ( hit reply limit )

but i’m saying any negative number aka if count is -1 it knows of the strings that make count–;

Milku86 - I get that but count knows of each card based on its value 1 or 0 or -1 each card is connected to 0 or 1 or -1 right? so what i’m trying to say is -1 = all the cases that make count decrement? right?

ieahleen - so yes i was right case “j”: is -1 so then count > 0 makes sense then to me. 10, “A”, “J”, “K”, or “Q” equals the count value of -1 so this is how that if statement knows of the strings…