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:
- 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.
- 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.