Counting Cards - What's the problem in the code?

Counting Cards - What's the problem in the code?
0.0 0

#1

What is it that is wrong in this code? I’m unable to find the problem.Following are the cases giving wrong result.

  1. Cards Sequence 2, J, 9, 2, 7 should return “1 Bet”
  2. Cards Sequence 2, 2, 10 should return “1 Bet”

var count = 0;

function cc(card) {

if (card>=2 && card<=6){
count++;
return “”+count+" Bet";
}else if (card>=7&&card<=9){
return “”+count+" Hold";
}else{
–count;
return “”+count+" Hold";
}

}

cc(2); cc(‘J’); cc(9); cc(2); cc(7);

Your Browser User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36.

Link to the challenge:


#2

Try using a switch statement with strategically placed breaks.

If you decide to go with your current data structure you may want to look at your return statements.

Happy Coding!


#3

Thanks for your advice but it’s just that the task is to solve the problem using object analysis and the thing is I didn’t get the card count concept. So it’s confusing when to get bet and when hold.


#4

Try separate your algo in two part counting and print the result


#5

With your current logic, you are only returning the count plus the word “Bet” if card is greater or equal to 2 and less than or equal to 6. Also, you are only returning the count plus the word “Hold” if the first if did not evaluate to true.

In this challenge you are always going to return the count (no matter what card is). The key to this challenge is understanding when to put the words “Bet”, “Hold” after the count. The words “Bet” and “Hold” have nothing to do with the card value, but instead depend on the value of count. You need to separate the logic of increasing or decreasing the count from the logic which adds the words “Bet” and “Hold” after count.