Counting Cards - wrong way?

Tell us what’s happening:
Describe your issue in detail here.

i don’t really catch the error…

  **Your code so far**

let count = 0;

function cc(card) {
switch (card) {
  case 2:
    count ++;
  case 3:
    count ++;
  case 4:
    count ++;
  case 5:
    count ++;
  case 6:
    count ++;
    break;
  case 7:
    count += 0;
  case 8:
    count += 0;
  case 9:
    count += 0;
    break;
  case 10:
    count --;
  case "J":
    count --;
  case "Q":
    count --;
  case "K":
    count --;
  case "A":
    count --;
  break;  
}
if (count > 0) {
  return count + " Beat";
} else {
  return count + " Hold";
}
}

console.log(cc(2, 3, 4, 5, 6));

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; rv:99.0) Gecko/20100101 Firefox/99.0

Challenge: Counting Cards

Link to the challenge:

let count = 0;

function cc(card) {

  switch (card) {

    case 2:

      count ++;

    case 3:

      count ++;

    case 4:

      count ++;

    case 5:

      count ++;

    case 6:

      count ++;

      

    case 7:

      count += 0;

    case 8:

      count += 0;

    case 9:

      count += 0;

      

    case 10:

      count --;

    case "J":

      count --;

    case "Q":

      count --;

    case "K":

      count --;

    case "A":

      count --;

      break;  

  }

  if (count > 0) {

    return count + " " + " Bet";

  } else {

    return count + " " +  "Hold";

  }

}

console.log(cc(2, 3, 4, 5, 6));

console.log(cc(3, 2, "A", "K"));

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

anche levando i primi due break, l’esercizio non viene risolto ma se faccio console.log(cc(Tutti i vostri esempi)) funziona. cosa sbaglio?

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

This:

console.log(cc(2, 3, 4, 5, 6));

won’t test properly - your function will just take the first parameter.

Let’s test it properly. If I give it this:

console.log(cc(2));
console.log(cc(3));
console.log(cc(4));

I would expect it to add 1 each time, so I’d expect the output to be:

1 Bet
2 Bet
3 Bet

Instead, I get this:

0 Hold
-1 Hold
-3 Hold

The issue is that you have no break statements in your switch.

To see what is happening, test it with this code:

let count = 0;

function cc(card) {
  console.log('\ncalling with card:', card)
  switch (card) {
    case 2:
      console.log('* match 2')
      count ++;
    case 3:
      console.log('* match 3')
      count ++;
    case 4:
      console.log('* match 4')
      count ++;
    case 5:
      console.log('* match 5')
      count ++;
    case 6:
      console.log('* match 6')
      count ++;
    case 7:
      console.log('* match 7')
      count += 0;
    case 8:
      console.log('* match 8')
      count += 0;
    case 9:
      console.log('* match 9')
      count += 0;
    case 10:
      console.log('* match 10')
      count --;
    case "J":
      console.log('* match J')
      count --;
    case "Q":
      console.log('* match Q')
      count --;
    case "K":
      console.log('* match K')
      count --;
    case "A":
      console.log('* match A')
      count --;
      break;  
  }

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

console.log(cc(2));
console.log(cc(3));
console.log(cc(4));

As always, check to docs. Seriously, always, always, always check the docs. Google “MDN switch” and read that. Especially about halfway down is a section on “Multi-case: single operation” - that’s going to be very useful.

By simplifying your code and using mulit-case and adding in a few breaks, I was able to get your code to work. There’s also a slight issue with the spacing on the output strings.

I thought that i had to inserti multiple data like
console.log((2, 3, 4, ...));
in that case is everything more simple with only one , done.
thank you for your advice sir, if you can clarify just the last think to me I would appreciate it.
counting today I have been looking for a way to make the program accept a multiple insertion for 3 days, what should I have done to make it?

ok, found switch - JavaScript | MDN. i’m gonna try alone.
Thank you for the precedent help.

If the function were set up that way, sure. But the way this function is written, it can only handle one parameter:

function cc(card) {

It will take one parameter, the first one. You could accept the second argument like:

function cc(card, card2) {

You could build this out this way, but you’d have to know the max number of cards. There are also ways to deal with variable numbers of parameters that you will learn later. In theory, you could write this function to do that, handle a variable number of parameters. But as written it can’t and it isn’t part of the assignment.

counting today I have been looking for a way to make the program accept a multiple insertion for 3 days, what should I have done to make it?

Again, the assignment is to handle one at a time. Finish that. Once you get that done, if you want, we can have some fun and make it work for a variable number of inputs.

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