# Counting Cards javascript problem

My code for this challenge follows. I`m not sure where I`m going wrong.
///

``````var count = 0;
var lowCards[2,3,4,5,6];
var medCards[7,8,9];
var highCards[10, 'J', 'Q', 'K', 'A'];
var allCards [lowCards, medCards, highCards];
function compareCard(card){
for(var i = 0; i < allCards.length; i++){ // goes through allCards
for(var j = 0; j<allCards[i].length; j++){// goes through each section of allCards
for(var lc = 0; lc < allCards[i].length; lc++){ //checks against lowCards
if (card == lowCards[lc]){
count += 1;
}
}
for(var mc = 0; mc < allCards[i].length; mc++){ //checks against midCards
if (card == midCards[mc]){
count = count;
}
}
for(var hc = 0; hc < allCards[i].length; hc++){ //checks against highCards
if (card == highCards[mc]){
count -=1;
}
}

}
}

if(count > 0){
return count + " Bet";
}
else{
return count + " Hold";
}
// Only change code above this line
}
function cc(card) {
// Only change code below this line
compareCard(card);
}
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');
``````

///

Iâve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The âpreformatted textâ tool in the editor (`</>`) will also add backticks around text.

I think either `switch` makes the most sense here. This is something I wrote for someone else working on this:

``````function isAVowel(letter) {
switch (letter.toLowerCase()) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
break
case 'y':
break
default:
}
}
``````

You need to do something similar, listing out the different cards and what action should be taken. I would suggest grouping cards 2-6 in one where you add 1 to count, then 10-âAâ where you subtract one, and then just default to do nothing (7-9).

The action you need to take is incrementing or decrementing count.

Let us know if this clears this up.

Thanks. ya I get what youâre doing, I was trying to do it with arrays and loops so I could make one that I could use for different size sets and basically have it work for whatever the array was storing. Like a more general approach that would work for any size with minimal adjustment to the code. Like go through this multidimensional array and check each index of each array with the input.

Well, if thatâs the case, one approach might be this:

``````var count = 0;
var lowCards = [2, 3, 4, 5, 6];
var highCards = [10, "J", "Q", "K", "A"];

function cc(card) {
if (lowCards.indexOf(card)!==-1)
count++;
else if (highCards.indexOf(card)!==-1)
count--;

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

Abstracting it a little further, this would work:

``````var count = 0;
var lowCards = {
list: [2, 3, 4, 5, 6],
};
var midCards = {
list: [7, 8, 9],
};
var highCards = {
list: [10, "J", "Q", "K", "A"],
};

var allCards = [lowCards, midCards, highCards];

function cc(card) {
for (let i = 0; i < allCards.length; i++) {
if (allCards[i].list.indexOf(card) !== -1) {
break;
}
}
if (count > 0)
return count + " Bet";
else
return count + " Hold"
}
``````

The key is to get rid of those inner loops. Why not use an array prototype method like indexOf?

Very clean solutions thank you. I like the usage of objects in the second more abstracted way. And youâre right. Index of is a much better approach to this. Thank you!

Yeah, I get your desire to understand more advanced design patterns, but in this case I think the `switch` solution is the ârightâ one - it is simple and easy to understand and you just learned it a few lessons ago - I think that is what they were expecting you to use.