Basic JavaScript: Counting Cards problem

Basic JavaScript: Counting Cards problem


in the exercise Basic JavaScript: Counting Cards I have this code:

var count = 0;

function cc(card) {
  // Only change code below this line
  if(card === 2 || card === 3 || card === 4 || card === 5 || card === 6){
    return count + " Bet";

  else if ( card === 7 || card === 8 || card === 9){
    return  count + " Hold";
  else if (card === 10 || card === "J" || card === "Q" || card === "K" ||card === "A" )
   return count + " Hold";


  // Only change code above this line

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

but I get these errors:

// running tests

Cards Sequence 2, J, 9, 2, 7 should return 1 Bet

Cards Sequence 2, 2, 10 should return 1 Bet

// tests completed

What is the problem?
Any ideas?

I think the problem is in your return statements. Whether the player bets or holds depends on the current value of the global count variable, not on the value of any one particular card. Count is updated 3-5 times in each test, and the final value of count (positive or 0/negative) should determine the message.

So for example, in this test: Cards Sequence 2, J, 9, 2, 7 should return 1 Bet your code is going to return the correct count of 1, but the message “Hold” (because if the card is 7, your code says to hold.)

Also, although it is possible to do this challenge with if statements, it is a great opportunity to make use of a switch statement to handle all the possible card values.

Hope that helps!

Your code’s issues revolve around the fact that you need to separate the logic for calculating the current count from the logic which returns the desired string (based on count). This means, the first part of your function’s code you deal with calculating the current count which depends on the value of card. The second part of your function will check the current value of count to determine the applicable string to return.

OK, thanks. It now works.