# Basic JavaScript: Counting Cards (Program not behaving as expected)

**URL:** <https://forum.freecodecamp.org/t/basic-javascript-counting-cards-program-not-behaving-as-expected/308666>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [August 24, 2019, 7:37am UTC](https://forum.freecodecamp.org/t/basic-javascript-counting-cards-program-not-behaving-as-expected/308666 "2019-08-24T07:37:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pamnanaimanish169](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pamnanaimanish169/32/126326_2.png) [@pamnanaimanish169](https://forum.freecodecamp.org/u/pamnanaimanish169)\
**Post date:** [August 24, 2019, 7:37am UTC](https://forum.freecodecamp.org/t/basic-javascript-counting-cards-program-not-behaving-as-expected/308666/1 "2019-08-24T07:37:00Z")

</div>

**Tell us what’s happening:**  
I am trying to complete the Counting Cards challenge using **if else if**  
loop but I am getting two resulats as positive

- Cards Sequence 7, 8, 9 should return `0 Hold` (Correct)
- Cards Sequence 10, J, Q, K, A should return `-5 Hold` (Correct)

but every other result is marked as incorrect. I also tried printing the count and result in a console.log statement ( **console.log(count + result)** ) and I am getting the same result as specified in the tests, but the program fails when I hit **Run The Tests**.

I also tried replacing return **count + result;** with return **`${count} ${result}`** with the following point in mind

> Do NOT include quotes (single or double) in the output.

Also, I tried the following logic:

```auto
      if ( count > 0 ) {
      return count + "Bet";
    } else if ( count <= 0 ) {
      return count + "Hold";
    }

```

as per this post:

[http://forum.freecodecamp.org/t/counting-cards-02/213077/13](http://forum.freecodecamp.org/t/counting-cards-02/213077/13)

but with no success.

Can anybody have a look and suggest something.

**Your code so far**

```js

var count = 0;
var result = "";

function cc(card) {
  // Only change code below this line
  
  // increment or decrement the global count variable according to the card's value
  if (card === '2' || card === '3' || card === '4' || card === '5' || card === '6') {
    count = count + 1;
    // console.log("Count after Incrementing " + count)
    
  } else if (card === '7' || card === '8' || card === '9') {
    count = count;
    // console.log("COunt after Same: " + count)

  } else if (card === '10' || card === 'J' || card === 'Q' || card === 'K' || card === 'A') {
    count = count - 1;
    // console.log("COunt after decrementing " + count)

  }

      if ( count > 0 ) {
      result = " Bet";
    } else if ( count <= 0 ) {
      result = " Hold";
    }

  // The function will then return a string with the current count
  // and the string Bet if the count is positive, or Hold if the count is zero or negative
   console.log(count + result)
  
  
  return count + result;
  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc('3')
cc('2')
cc('A')
cc('10')
cc('K')

```

**Your browser information:**

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

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/counting-cards)**
>
> Learn to Code — For Free

---

<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:** [August 24, 2019, 7:44am UTC](https://forum.freecodecamp.org/t/basic-javascript-counting-cards-program-not-behaving-as-expected/308666/2 "2019-08-24T07:44:31Z")

</div>

> [@pamnanaimanish169](#):
>
> card === ‘2’

when the card has a numeric value then it will be a number, and `2 === '2'` is false.

> [@pamnanaimanish169](#):
>
> count = count;

this is not wrong, but as you are not doing anything with this you can just avoid this else if statement

---

<div class="post-metadata">

**Author:** ![MakingStuffs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/makingstuffs/32/127499_2.png) [@MakingStuffs](https://forum.freecodecamp.org/u/MakingStuffs)\
**Post date:** [August 24, 2019, 7:49am UTC](https://forum.freecodecamp.org/t/basic-javascript-counting-cards-program-not-behaving-as-expected/308666/3 "2019-08-24T07:49:40Z")

</div>

EDIT: You do not _need_ ES6 back-ticks but could construct your return statement with them as follows:

```auto
if ( count > 0 ) {
  result = `${count} Bet`;
} else if ( count <= 0 ) {
  result = `${count} Hold`;
}

```

The next issue with your code is that you are wrapping the numbers in your arguments in single quotes. This turns them into a string which means that `card === '6'` would never return true.

You need to make sure your if statements are checking for numbers by simply putting the number, for example:

```auto
  if (card === 2 || card === 3 || card === 4 || card === 5 || card === 6) {
    count = count + 1;
    // console.log("Count after Incrementing " + count)
  }

```

---

<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:** [August 24, 2019, 7:53am UTC](https://forum.freecodecamp.org/t/basic-javascript-counting-cards-program-not-behaving-as-expected/308666/4 "2019-08-24T07:53:36Z")

</div>

> [@MakingStuffs](#):
>
> From what I can see you need to construct your final answer with ES6 back-ticks.

it is not necessary, you can totally concatenate strings without using that - also because it has not yet been presented in this part of the curriculum

---

<div class="post-metadata">

**Author:** ![MakingStuffs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/makingstuffs/32/127499_2.png) [@MakingStuffs](https://forum.freecodecamp.org/u/MakingStuffs)\
**Post date:** [August 24, 2019, 7:59am UTC](https://forum.freecodecamp.org/t/basic-javascript-counting-cards-program-not-behaving-as-expected/308666/5 "2019-08-24T07:59:50Z")

</div>

Ah my bad! I misread the question.

---

<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 25, 2021, 12:19pm UTC](https://forum.freecodecamp.org/t/basic-javascript-counting-cards-program-not-behaving-as-expected/308666/6 "2021-01-25T12:19:59Z")

</div>


