Basic JavaScript: Counting Cards [Help!]

Basic JavaScript: Counting Cards [Help!]
0.0 0

#1

I have seen multiple solutions using "switch " and some "else if’ but I really want to make my code work. Would you mind taking a look? Thank you!

var count = 0;

function cc(card) {
  // Only change code below this line

 if (card <=  6 && card <= 2) {
  count  = count + 1 ;

  return  count  + "Bet"
  }
  else  if (card <= 9 && card >= 7){ 
 count = count  +  0 ;

 return count + "Hold"

  }

  else if ( card = 10  ||  'J'  || 'Q ' || 'K' || 'A') {
    count = count - 1 ;
    return count  + "Hold"
  }
 
  return "Change Me";
  // 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');


Counting Cards (Still stuggling)
#2

You have a logic mistake in the above line. This if statement will only evaluate to true if the card is a 2.

You can not make multiple comparisons like you are trying to do above. You must compare each to card. Also, do not confuse the assignment operator with an equality operator. You may need to go back a few lessons to review the differences between the two operators.

Lastly, you need to separate the count increase/decrease logic from the logic around what to actually use in the return statement. Currently, you are mixing the two together which is a big problem. What determines a " Hold" or " Bet" is not dependent on the current card value. It is definitely dependent on the count value though.


#3

randelldawson,

Thank you for your swift response. Your insights are absolutely helpful.


#4

Tell us what’s happening:
I have been tackling this challenge for a few hours and I can’t get why it does not accept the following conditions:

Cards Sequence 10, J, Q, K, A should return -5 Hold
Cards Sequence 3, 7, Q, 8, A should return -1 Hold
Cards Sequence 3, 2, A, 10, K should return -1 Hold

Your code so far


var count = 0;
var answer = ''

function cc(card) {
  // Only change code below this line

 if (card >=2 && card <=6)  {
  count  = count + 1 ;

  }
  else  if (card >= 7 && card <= 9)  { 
 count = count  +  0 ;

  }

  else if ( card == 10||'J'||'Q'||'K'||'A') {
    count = count - 1 ;
  }
 
 if (count > 0) {
answer = (count) + " Bet";

} else if (count === 0) {
answer = (count) + " Hold";
}
else if (count <= 0) {
answer = (count) + 'Hold';
}

return answer;
  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(10); cc("J"); cc("Q"); cc("K"); cc("A");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/counting-cards


#5

have you tried adding a bunch of console.log and running your code to see if you are getting what you expect at different points in the program? That’s what I would be doing…


#6

Thank you. I will definitely try your suggestion.


#7

ok, just fyi, i noticed you forgot a space for one of the Hold return strings…


#8

Thank you for that observation.