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

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:

      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

but with no success.

Can anybody have a look and suggest something.

Your code so far


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:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/counting-cards

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

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

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

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:

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

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

Ah my bad! I misread the question.