No Numbers Counted - Basic JavaScript - Counting Cards

Tell us what’s happening:
I have written solutions with bother if/else and switch statements however it would just count the strings and not the numbers in the array provided.

Typical output would look like this:
0
0
0
-1
-2

The first 3 0’s are for the numbers in the input array.

  **Your code so far**
let count = 0;

function cc(card) {
// Only change code below this line

for (let i = 0; i < card.length; i++) {
  console.log(typeof(card[i]));
  if (card[i] >= 2 && card[i] <= 6) {
    count++;
  } else if (card[i] == 10 || card[i] == 'J' || card[i] == 'Q' || card[i] == 'K' || card[i] == 'A') {
    count--;
  }
}

console.log(count);
if (count > 0) {
  return count + " Bet";
} else {
  return count + " Hold";
}

// Only change code above this line
}

cc(2); cc(3); cc(7); cc('K'); cc('A');
  **Your browser information:**

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

Challenge: Basic JavaScript - Counting Cards

Link to the challenge:

I don’t understand your thinking here:

for (let i = 0; i < card.length; i++) {

Do you think card is an array? The point is that you feed this function one card at a time, as a number or a string. There is no need for a loop for this function - it will only handle one piece of data at a time.

1 Like

Hi kevinSmith,

You’re right, each call to the function is actually separate, I don’t know how I didn’t see that. I have just solved it right now.

Thank you.

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