Js counting cards

Tell us what’s happening:

  **Your code so far**

var count = 0;

function cc(card) {

if (card = 2 || 3 || 4 || 5 || 6) {
return count++;
}
else if (card = 7 || 8 || 9) {
return count = 0;
}
else if(card = 10 || 'J' || 'Q' || 'K' || 'A') {
return count--;
}
var holdbet = "Hold";
if (count > 0) {
return count + "" + holdbet;
}
}

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

what should I change in order to let this program works?

  **Your browser information:**

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

Challenge: Counting Cards

Link to the challenge:

You shouldn’t set count to 0 here. You should add 0 to the count.

Do NOT reset count to 0 when value is 7, 8, or 9.

1 Like

can you show me the correct code ?

Nope. We don’t give out solutions.

But, you are pretty close. You need to not set count to 0.

Once you have fixed that, you have a few more bugs.

Returning from a function stops the execution. If you return the count, then the rest of the function never executes.

I don’t see "Bet" here anywhere. In which case should you return a string with "Bet"?

This is an empty string. Why are you putting an empty string in between your count and your holdbet string?

var count = 0;

function cc(card) {

if (card = 2 || 3 || 4 || 5 || 6) {

 count++;

}

else if (card = 7 || 8 || 9) {

 count;

}

else if(card = 10 || 'J' || 'Q' || 'K' || 'A') {

 count--;

}

var holdbet = 'Hold';

if (count > 0) {

    holdbet = 'Bet';

  }

 return count + " " + holdbet;

}

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

This still doesn’t work

the empty string is for the space

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.

You can also 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 (’).

1 Like

An empty string "" is different than a space " ". It looks like you fixed that though.

Real close. Question: how do you check multiple conditions? (Hint: this isn’t it :smiley: )

with the || (OR) operator…I’ve also tried with the && operator but it returns me the same inefficiency…

The || operator checks

(full logical conditional) || (other full logical conditional)

Are you comparing full conditions?

well, first you need to remember that = is the assignment operator, you are assigning a value to the variable, not comparing it

also || works as condition1 || condition2, your condition2 is 3 which is not complete as a condition

1 Like
var count = 0;

function cc(card) {

if ((card = 2) || (card = 3) || (card = 4) || (card = 5) || (card = 6)) {
 count++;
}
else if ((card = 7) || (card = 8) || (card = 9)) {
 count;
}
else if ((card = 10) || (card =  'J') || (card = 'Q') || (card = 'K') || (card = 'A')) {
 count--;
}
var holdbet = 'Hold';
if (count > 0) {
    holdbet = 'Bet';
  }

 return count + " " + holdbet;
}

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

This still doesn’t work…:frowning:

the first issue I pointed out still stands

There is a big difference between =, == and ===.

var count = 0;

function cc(card) {

if ((card = 2) || (card = 3) || (card = 4) || (card = 5) || (card = 6)) {
 count = count + 1;
}
else if ((card = 7) || (card = 8) || (card = 9)) {
 count = count + 0;
}
else if ((card = 10) || (card =  'J') || (card = 'Q') || (card = 'K') || (card = 'A')) {
 count = count - 1;
}
var holdbet = 'Hold';
if (count > 0) {
    holdbet = 'Bet';
  }

 return count + " " + holdbet;
}

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

where i am wrong ?

you are still not checking the value of card but assigning a new value to it

Did you look up the difference between =, == and ===? You seem to not be listening to @ilenia.

1 Like

Thank you guys…sorry for being a little stupid

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