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

**URL:** <https://forum.freecodecamp.org/t/counting-cards-challenge-lets-compare-our-code/74956>\
**Category:** General\
**Created:** [January 10, 2017, 12:21pm UTC](https://forum.freecodecamp.org/t/counting-cards-challenge-lets-compare-our-code/74956 "2017-01-10T12:21:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pufanul](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pufanul/32/163858_2.png) [@Pufanul](https://forum.freecodecamp.org/u/Pufanul)\
**Post date:** [January 10, 2017, 12:21pm UTC](https://forum.freecodecamp.org/t/counting-cards-challenge-lets-compare-our-code/74956/1 "2017-01-10T12:21:34Z")

</div>

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

I just completed the [Counting Cards Challenge](https://www.freecodecamp.com/challenges/counting-cards) 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:

```auto
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
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc('J'); cc(9); cc(2); cc(7);

```

Cheers,  
Alexandru.

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [January 10, 2017, 1:36pm UTC](https://forum.freecodecamp.org/t/counting-cards-challenge-lets-compare-our-code/74956/2 "2017-01-10T13:36:39Z")

</div>

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' || ...)`)

```auto
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.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:08pm UTC](https://forum.freecodecamp.org/t/counting-cards-challenge-lets-compare-our-code/74956/3 "2021-01-16T17:08:53Z")

</div>

Closed on January 16, 2021, 5:08pm UTC


