# Basic JavaScript - Counting Cards

I don’t understand why in the if case for the numbers 7, 8, 9 the count is showing 2 instead of 0. Thanks

``````let count = 0;

function cc(card) {
// Only change code below this line
const hold = " Hold";
const bet = " Bet";
const alphabet = ["J", "Q", "K", "A"];

if (card >= 2 && card <= 6) {
count++;
console.log(count + " cadena 2, 3, 4, 5, 6");
}
else if(card >= 7 && card <= 9){
count += 0;
console.log(count + " cadena 7, 8, 9");
}
else if (card === 10 || alphabet) {
count--;
console.log(count + " cadena 10, 'J', 'Q', 'K', 'A'");
}

if(card > 0){
return count + bet;
}
return count + hold;
// Only change code above this line
}

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

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

Challenge: Basic JavaScript - Counting Cards

You are checking the wrong variable here (you should be checking the value of count):

Also, you have faulty logic here (though you will still pass the challenge):

`if (alphabet)` will always return true because alphabet is defined.

`if (card === 10 || card === alphabet)` would also fail because that’s comparing the value of card with the whole array.

If you want to check if the card matches a value within the alphabet array, you need to explicitly do this.

The only reason your function passes (once you’ve corrected the first variable check) is because of the order of your conditional statements.

1 Like

Thanks @igorgetmeabrain

I ended up solving it as you told me. Removing the coercion bug and replacing the wrong variable card for count.

let count = 0;

function cc(card) {
// Only change code below this line
const hold = " Hold";
const bet = " Bet";
const alphabet = [“J”, “Q”, “K”, “A”];

if (card >= 2 && card <= 6) {
count++;
console.log(count + " cadena 2, 3, 4, 5, 6");
}
else if(card >= 7 && card <= 9){
count += 0;
console.log(count + " cadena 7, 8, 9");
}
else if (card === String(10) || alphabet) {
count–;
console.log(count + " cadena 10, ‘J’, ‘Q’, ‘K’, ‘A’");
}

if(count > 0){
return count + bet;
}
return count + hold;
// Only change code above this line
}

cc(2); cc(3); cc(7); cc(‘K’); cc(‘A’);

What coercion bug? You don’t need to coerce anything into a string.

The issue is with the array alphabet.
Your conditional statement currently only checks if alphabet is defined. It doesn’t compare it with the value of card.
Essentially it’s shorthand for:
`if (card === 10) || if (alphabet)`

To explicitly make a comparison with alphabet, your code would have to be:
`if (card === 10 || card === alphabet)`

But that would check if card is equal to [“J”, “Q”, “K”, “A”] (i.e. the whole array), not to any single value within that array. So you need to find a way of checking if the value of card matches any of the values within the array. You could use indexOf() for this perhaps.

If you swap the order of the if else statements, you’ll see that your code breaks, because this conditional statement is faulty. Also, it’s a minor point, but the conditional statement checking for values 7-9 is actually redundant anyway. You don’t actually need to include it at all, as it doesn’t actually do anything.

Is this correct? Checking each value of the array to see if it matches.

else if (card == 10 || card == “J” || card == “Q” || card == “K” || card == “A”) {
count–;
console.log(count + " cadena 10, ‘J’, ‘Q’, ‘K’, ‘A’");
}

Yes, that’s another way of doing it.
`count-` is a syntax error though. You’ve lost a -.

1 Like

Thanks a lot. Fixed

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