Counting Cards: code not fully correct

Tell us what’s happening:
Hi, my code is partially executing. Can someone point out my mistakes?

Your code so far


var count = 0;

function cc(card) {
  // Only change code below this line
  if (card == 2 || card == 3 ||card == 4 || card == 5 || card == 6){
    count++;
    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'){
    count--;
    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');

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

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

Hey,

Right now you’re ending the function by returning count + string, meaning that the last function will determine what the string will be regardless of the count value.

Here’s an example:

cc(2) cc(2) cc(2) cc(A)

The count will equal 2, so it’s positive and player should bet, but because of the last function the count value went down, it returned “2 Hold”.

To fix this you want to keep changing the count value as you are, but only return the count + string at the end of the function.

Hi Gigusek,
Thanks for the explanation. I still have two questions,

  1. Why are we calling function cc so many times in the code?
  2. Do you mean that every time we write return statement, the code returns to the function?

I think some more explanation on return statement part would be extremely helpful.

Thanks again!

Hi Yogita,

Generally speaking it’s good practice to let functions have only one return statement. This makes them easier to read and understand. There are exceptions of course.

The cc function is called multiple times because that’s part of the assignment. The cc function gets a single card each time it is called and you keep count by using the count variable that’s outside of the function.

The return statement works as follows:

1
return statements can only be used inside a function (or methods, which are functions too)

2
When the JavaScript engine executes a return statement two things happen:
a: the function is exited, this means the rest of the code in the function, however many lines it is (0 or 5000) is not executed. See example 1 below.
b: the value that has been given to the return statement is passed back to the code that called the function. See example 2 below.

Example 1:

function hello () {
  return "hi!";
  var foo = 4;
  if (foo === 4) {
    return "bye";
  }
}

In this example all of the code after return "hi!"; is never executed.

Example 2:

function double (num) {
  return num * 2;
}

var result = double(14);  // 28

In this example we pass in the value 14, when the return statement is executed, the value that is given to return is passed back to the “caller” of the function.

Going back to your code:
Your code currently returns the value of count (which is correct) but then it returns “Bet” or “Hold” depending on the value of the last card. The assignment on the other hand asks you to look at the current value of count to decide whether to send back “Bet” or “Hold”.

Does that help?

Bonus tip:

  • You can skip the if statement where card is equal to 7, 8 or 9, as it has no effect on the count.
  • Put indeed the return statement at the end to present the correct “Bet” or “Hold” result.
  • You might use switches instead of if statements, but that is of course what you prefer.

Thanks for such a detailed explanation nielsborn.

  1. function of return command is clear to me now. Taking your suggestion, I have removed multiple return statements from my code and have included a single if-else loop to return the output in the end. I am pasting the revised code here, can you please check it?

  2. Can i replace return command with console.log() here? Can you also explain where can I replace return with console.log and where I can’t?

var count = 0;

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

Hi rogierv,
I have included all these changes except changing to switch but the code still doesn’t work.

Although your explanation is clear to me, I just want to confirm my understanding on calling cc() function multiple times . Out of all the calls, the last call that is cc(“A”) will be returned, right?

As @camperextraordinaire says: you’re very close now.

I’ve got a couple of small remarks.

function of return command is clear to me now. Taking your suggestion, I have removed multiple return statements from my code and have included a single if-else loop to return the output in the end. I am pasting the revised code here, can you please check it?

1
“if-else loop”
We usually call this an “if-else statement”. Loops are things like “for”, “while”.

2
Currently your function has two return statements.

I would say it’s better to have just one return statement in a function.
Something like:

var result = count;
if (count > 0) {
  result += " Bet";
} else {
  result += " Hold";
}
return result;

3

Can i replace return command with console.log() here? Can you also explain where can I replace return with console.log and where I can’t?

I think you slightly misunderstand what console.log();does.
console.log(); is used for debugging purposes only. It’s a way of displaying in the browser (or command line) what’s going in the JavaScript code.

Almost always console.log(); does not have any effect on what the rest of your code is doing.
This also means you cannot use console.log(); instead of return. They do different things :slight_smile:

4

Out of all the calls, the last call that is cc(“A”) will be returned, right?

No. Each time the function is called it will return a value. That’s how you wrote the function and that’s how it should work.

But: the assignment will only check the return value of the last call to the function. That last call is (as you write) cc("A").

Side note: I think the assignment could have been written a little clearer. (I should create a PR for this)

5
To help you with coding: definitely use console.log();. Whenever you want to see the value of a variable in your code, just console.log(foo); to see the value of variable foo.

Good luck!

Not exactly, the function will execute its code and return something (as long as it contains a return statement) each time it’s called. The thing is, because of how you’ve written your code in the first post, the string (“Hold” or “Bet”) the function will return is dependant on the passed-in card instead of the value of the counter.

I’ll give you 2 examples:

  1. Using your own code
cc(A); cc(A); cc(2)

The function will be executed 3 times, remember that counter starts at 0:
cc(A) - the counter is -1 - the return value is "-1 Hold"
cc(A) - the counter is -2 - the return value is  "-2 Hold"
cc(2) - the counter is -1 - the return value is "-1 Bet"

You see, despite the counter yielding a negative number (-1) it still tells you to bet, which is incorrect in regards to the challenge. That’s why your code isn’t passing it, because, your function is telling us to bet/hold depending on the current card and not the counter, which actually makes the counter irrelevant.

What we want to do is count the card, update the counter and then check the counter to decide whether we should bet or hold. So let’s move on to the 2nd example.

  1. After making some changes to your code.
if (card == 2 || card == 3 ||card == 4 || card == 5 || card == 6){
    count++
} else if (card == 10 || card =='J' || card =='Q' || card =='K' || card =='A'){
    count--
} 

if (count > 0) {
    return count + ` Bet`
} else {
    return count + ` Hold`
}

What did we do? First of all, we removed the return statement from each code block, because this part of the code will be only responsible for changing the counter value depending on the card we’re passing in. We have also removed one of the (else) if statements as we didn’t need it because 7/8/9 cards didn’t change the counter anyway.

The 2nd thing we did was add a new set of if statements. This one will be responsible for actually returning a value and ending the function. The condition in the new if statement the current counter value, which makes the return value completely dependant on the counter and not the current card.

Let's execute this code 3 times with the same cards as before.

The function will be executed 3 times, remember that counter starts at 0:
cc(A) - the counter is -1 - the return value is "-1 Hold"
cc(A) - the counter is -2 - the return value is  "-2 Hold"
cc(2) - the counter is -1 - the return value is "-1 Hold"

Now we’re getting the correct outcome. We’re still passing in and counting the cards, but this time the function counting the card and returning a value are separate processes. This time we’re looking towards the counter and not the card, to see if we should bet or not.