Verify code - Counting Card - where is the mistake?

Hi World,

could you help me please?, I´m stucked with this challenge. I´ve watched the video and others possible solutions, and I would like to understand why my “idea” doesn´t works (I thought it should work…)

image

Please, if you have enough patient to clarfy the reassons I would be really thankfull with you.

Best regards.

Hi @titokanito88 !

First off, It is always better to post your code instead of screenshots.

But your logic doesn’t work for the problem.

if (card >=2 || card <=6)

What if a card is 7,8,9,10, ‘J’, ‘Q’, ‘K’, ‘A’?
Then by your code it would still increment because all of those inputs are greater than 2.

Also there is an issue with this logic

else if (card !== 2,3,4,5,6)

What if the card if 7,8, or 9?
It should not decrement.

I think what would help is write out in plain english what you want to accomplish then translate that to code.

1 Like

Really appreciated for your answer jwilkins.oboe!,

I´ll try to explain what I would like to do with my code:

we start with count = 0, and we want to asign a value +1 if the cards are between 2 - 6.
That´s why I wrote if (card >= 2 || card <= 6) each value between 2 and 6 means:
2, 3, 4, 5 and 6.

Next to this, I declared that count shlud added + 1 in to count value: count ++;

Well, until this point, is it right?

inside my logic process (maybe completely wrong), if the values from cards 7, 8 and 9 are not relevant (cause count will not be anyway modified) due added value = 0, why should we declare something for they?

The other cases we need to declare are cards from 10 until ‘A’, which decrease the count - 1 each card. Well, if we declare that all values different than: 2, 3, 4, 5 and 6 should decrease the count with -1 it should works…right? at least we want to have a finally acount result diferent from 0 to return “Bet” or “Hold”.

Then I wrote else (count < 0) return count + " Hold". Here I tried to declare that each value under 0 equal “Hold”. Is should work, isn´t it?

Side note, you shouldn’t have a space between count and ++.


if (card >= 2 || card <= 6) each value between 2 and 6 means:
2, 3, 4, 5 and 6.

Your words and code don’t match. I highlighted a very important word.


Your other two if clauses have serious syntax issues. I I think it would be worth it to go back and look at the earlier if-else statement challenges because both the syntax and logic expressed in your else if and else clauses do not do what you think they do.

2 Likes

Actually what you wrote was this

If card is greater than or equal to 2 OR if card is less than or equal to 6.

I think you meant to use a different operator because you want to say

If card is greater than or equal to 2 AND if card is less than or equal to 6.

2 Likes

Hi JeremyLT, thank you for your support,

I´ll take again the previous if- else statements and do it again.

Related to my incongruents word and explanation, I want to reffer that 2 and 6 are included into values (from 2 until 6). Sorry if I don´t explain it correctly.

jwilkins.oboe, thanks again,

I´ll try something different. Thank you very much for your help.

You said you want values that are between 2 and 6. Your code says values that are bigger than or equal to 2 or smaller than or equal to 6.

1 Like

You are on the right track.
And we get what you are trying to do but the code is not matching up with your words.

Try to tackle this first part by choosing an operator that makes sense.

Then tackle the next part

If card is 10,J,Q,K, or A decrement card by 1

Then you can tackle the last part with the condition and return statements.

1 Like

So, I´m back :slight_smile:

I´ve this, but I still not passing the challenge…

var count = 0;

function cc(card) {
  // Only change code below this line
if (card >=2 && card <=6) {
  count++;
} else if (card == 10, 'J', 'Q', 'K', 'A') {
  count--;
} else if (card == 7, 8, 9) {
  count = 0;
}
if (count <= 0) {
  return count + " Hold";
} else
  return count + " Bet";

  // Only change code above this line
}

Here my new code, and now I´ve included a declaration for values 7, 8 and 9.

Where is my mistake? Sorry, I don´t understand, and I don´t want to use the switch metod to solve the challenge. Could you help me?

Getting closer!

This isn’t how you check that card matches multiple values.

1 Like

I´ve it!

var count = 0;

function cc(card) {
  // Only change code below this line
if (card >=2 && card <=6) {
  count++;
} else if (card == 10 ||card =='J' ||card== 'Q' ||card == 'K' ||card == 'A') {
  count--;
} 
if (count <= 0) {
  return count + " Hold";
} else
  return count + " Bet";

  // Only change code above this line
}

OMG!, really? The mistake was the “f*ing” commas! I was separating values wrongly :sweat_smile:

OK, OK, I´ll never forget it! (hopefully).

Thanks for your support!

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.