Can't solve issue with this Blackjack Card Counting code!

Tell us what’s happening:
Hey all! Hope you’re doing well, can’t seem to figure out why the below code isn’t working? Any and all help appreciated :slight_smile: Thank you!

  **Your code so far**

var count = 0;

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

var checker = [10, "J", "Q", "K", "A"];

if (card > 1 && card < 7) {
count++;
}else if (checker.includes(card) == true){
count--;
}

if (count > 0) {
console.log(count + " Bet");
}else if (count <= 0) {
console.log(count + " Hold");
}

// Only change code above this line
}

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/90.0.4430.212 Safari/537.36

Challenge: Counting Cards

Link to the challenge:

What do the failing tests say?

1 Like

Keep in mind that returning something from a function and console.logging something in a function aren’t the same thing…

2 Likes

This is what I get from the console:

// running tests Cards Sequence 2, 3, 4, 5, 6 should return

5 Bet

Cards Sequence 7, 8, 9 should return the string

0 Hold

Cards Sequence 10, J, Q, K, A should return the string

-5 Hold

Cards Sequence 3, 7, Q, 8, A should return the string

-1 Hold

Cards Sequence 2, J, 9, 2, 7 should return the string

1 Bet

Cards Sequence 2, 2, 10 should return the string

1 Bet

1 Bet 2 Bet 2 Bet 1 Bet 0 Hold Cards Sequence 3, 2, A, 10, K should return the string

-1 Hold

// tests completed // console output 1 Bet 2 Bet 3 Bet 4 Bet 5 Bet 1 Bet 2 Bet 2 Bet 1 Bet 0 Hold 0 Hold 0 Hold 0 Hold -1 Hold -2 Hold -3 Hold -4 Hold -5 Hold 1 Bet 1 Bet 0 Hold 0 Hold -1 Hold 1 Bet 0 Hold 0 Hold 1 Bet 1 Bet 1 Bet 2 Bet 1 Bet 1 Bet 2 Bet 1 Bet 0 Hold -1 Hold

  • Note that for the reponses above, each of them (e.g.: 1 Bet) is on a separate line to another.

Sorry, I didn’t truly understand the difference when it was explained - from my perspective return acts akin to a ;break, would that be right? Also, how would the two differently react to my code above? Thank you!

All the failing tests say that your function “should return” something. I don’t see a return statement in your function.

All I had to do to fix it was change the console.log output to the following and it worked …:

return (count + " Bet");

My bad! Apologies but would you mind explaining the difference between the two? As I mentioned above what I understood from return functions is that it activates akin to a (;break) function if triggered?

Return takes the value on that line and "return"s it back to the calling context. This is the way that you get information from inside your function to outside your function.

1 Like

A return exits the function (no further function code is executed) and emits (“returns”) a value. This is what allows code to call a function to get (or generate) some piece of data.

For example, I believe that you have used Math.random()

let someNum = Math.random();

The reason that a random number gets put in someNum in this case is because the function Math.random() returns that number.

console.log() only prints to the JavaScript console. It doesn’t impact the function in any way.

I’m genuinely confused about how you got this deep into the curriculum without knowing what a return statement does. Did you skip ahead in the challenges? Did we say something confusing when we introduced return or when we’ve used it in the intervening challenges?

I’m seriously and honestly curious. People seem to get to these challenges without knowing what return does and I don’t understand how/why this happens or what we can do to help make sure it doesn’t happen so often.

@JeremyLT I ask about this in an issue at one point.

We didn’t really get to anything, but I guess that’s partly my fault as well. I just don’t know how to best handle it. The problem is we can’t (as far as I know) add new challenges, so it has to happen through updates to the existing ones.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.