# Counting Cards Challenge - let's compare our code!

Hello fellow coders,
What a nice day to have a discussion isn’t it?

I just completed the Counting Cards Challenge but I feel like my code is really sloppy and it can be improved, that’s why I have several questions:

• I added too many lines of code?
• Is there something I could have done better?
• Is the switch statement better than the else if statement?

Also I’m curious to see how you approached this challenge. What was your code like?
Here is mine:

``````var count = 0;

function cc(card) {
// Only change code below this line
if (card >= 2 && card <= 6) {
count++;
} else if (card >= 7 && card <= 9) {
count+= 0;
}
else {
count--;
}

if (count <= 0) {
return count + " Hold";
} else {
return count + " Bet";
}
// Only change code above this line
}

// Note: Only the last will display
cc(2); cc('J'); cc(9); cc(2); cc(7);
``````

Cheers,
Alexandru.

I used a `switch` instead of `if-else`, since there’s only a few values `card` can take, and there’s only 2 scenarios to consider: incrementing or decrementing `count`. There’s more lines, but I think writing this using `if-else` can get ugly quickly (like: `(card == 10 || card == 'J' || card == 'Q' || ...)`)

``````function cc(card) {
// Only change code below this line
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count--;
break;
}

if (count > 0) {
return count + " Bet";
} else {
return count + " Hold";
}
// Only change code above this line
}

``````

EDIT: Also, I blurred your code since it’s a spoiler.

1 Like