How to think in complex quiz like Counting Cards

Why I cannot think about what to do as I saw the hint and see how it work, yet it is hard for me to understand it. It is like I get brain fog. Why is that and how to overcome it?


var count = 0;

function cc(card) {
  // Only change code below this line
  
  
  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');

Your browser information:

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

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

Don’t start out thinking about how to impliment a solution via code. First break down the problem and make sure you understand it. Then find the logical solution without worrying about the computer. If you had to give a human very specific an detailed instructions, how would you do it. Once you have that logical structure, you can translate it it JavaScript.

4 Likes

Think of an actual deck of cards. The function you are writing will be called several times, which is represented by cc(2); cc(3); cc(7); cc(‘K’); cc(‘A’); Each time the function is called represents one card shown. Based on the cards that have been shown (2, 3, 7, K, A), should we BET or HOLD? In order find out we ‘count’ the cards. There are 3 possibilities we are tracking:

+1 Low Card (2, 3, 4, 5, 6 )
+0 Neutral Card (7, 8, 9)
-1 High Card (10, ‘J’, ‘Q’, ‘K’, ‘A’)

If a low card is shown, you should increase the global variable ‘count’ by 1. If Neutral card do nothing, if high card decrease global variable ‘count’ by 1.

The count variable is resting outside the function because this will allow us to count multiple cards and give a recommendation for BET or HOLD at any given time, regardless of how many cards have been shown.

I hope this helps. Happy coding!

1 Like

What wonderful advice. Thanks Nancy

@ArielLeslie Thanks, ur advice was very helpful. Now following is my code but I am stuck. My first two cases work, but next 2 doesn’t. I wonder what I am doing wrong here? I can try to get hint and I did but this way I am getting more understanding of why should I and shouldn’t I use switch or if/else.

var count = 0;

function cc(card) {
  // Only change code below this line
if(card >=2 && card <= 6){
  console.log(count+=card);
  return  "5 Bet"
}else if(card >=7 && card <=9){
  return "0 Hold"
}else if((card == 10) || (card == 'J') || (card == 'Q') || (card == 'K') || (card == 'A')){
  console.log("-5 Hold");
}else if((card == 3) || (card == 7) || (card == 'Q') || (card == 8) || (card == 'A')){
  console.log("-1 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');

@mahassan You should not be trying to hard-code the return values. Your logic should build the string necessary to return the correct string values (regardless what values are passed to the function in the tests). Just pretend you don’t know what values are going to be coming in, because your function will not no either.

My suggestion is to break the function into two parts. The first part of the function will contain the logic necessary for increasing/decreasing the count. The second part of the function will contain the logic which actually returns the necessary string. The second part depends on you calculating the correct count in the first part, so get that logic right first and then figure out the return statement(s) needed for returning the string values.

2 Likes

I will give it a go tomorrow.

WAOOOO BRAVOOOOO thank you

Wish I had seen all these before peeking the answers Peeked so much that I have run away from them