Why doesn't this code work?

Tell us what’s happening:
I came pretty close to getting the correct answer according to the solution on the reveal. But I was interested in watching the video first so I replicated that person’s method of using the variable holdbet. For some reason my code is not working.
I am not really sure why though, I have reviewed it a few times. Can anyone help me identify where this problem is?

  **Your code so far**

var count = 0;

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

switch(card){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
  count ++;
  break;

case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
  count --;
  break;
}
var holdbet = "Hold";
if (count > 0) {
holdbet = "Bet";
}

return count + "" + holdbet;
// 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/91.0.4472.164 Safari/537.36

Challenge: Counting Cards

Link to the challenge:

This is the problem with copying others. What is this line doing? What specifically is the "" doing?

2 Likes

Hi, so I wasn’t 100% sure on the exact reasoning behind why the code is laid out this way. I’d like to have researched how it makes sense since it is new to me. But the entire block of code is an exact replication of what the guy does in the video. So if it doesn’t make sense, how could it work for him and why doesn’t it work for me if it does make sense?
Here is the screenshot I took from the video for this lesson. I have also removed the ; where I have them and he doesn’t after ‘Hold’ and ‘Bet’.

your screenshot is not exactly the same,.
Hint: sometimes the difference is a space.

2 Likes

Again, you have not exactly copied the video. The "" is different. This is the problem with copying without understanding. It makes it harder to see bugs.

So… What do you think that part might do? Why would they add an empty string? Should the string be empty?

1 Like

since you are learning let’s try to explain this to you.
the code is laid out in a switch statement, switch is essentially a neater way to set up a strict equality if…else… statement. the code goes through the numbers saying if card number is less than or equal to 6 increment the count else if the card is 10 or higher decrement the count.
your error looks to be in the return statement, you are saying return count concatenating an empty string and then concatenating holdbet.
so if that was returned it would look something like this I assume…: -1hold.
see the problem with that return? now in the video there is a space in string which if entered would allow the return to be : -1 hold.
it’s always best to understand the material before moving on to the next section :slight_smile: I hope this helped

1 Like

I understand what you are saying. No need to come at me so harshly for it. I have literally JUST started programming I understand you’re familiar with the programming world as with annoying people that copy other people’s without understanding it. But I am very new and after being so close to the solution without help, but not able to pass the test, I followed the video. My intention wasn’t just to move on without fully understanding all of his code if I didn’t already. But that is for me to figure out and I guess now I just feel dumb for not.

That makes complete sense to me now. I feel so dumb. Thank you! I will try to make sure I understand the code I have implemented before asking why it doesn’t work.
Thank you

Thank you! Idk why I missed that space XD

1 Like

I’m not trying to be harsh and I’m sorry I came across that way.

I asked you a question about the code to get you thinking about a specific part of the code where the problem was, and you didn’t answer my question, so I repeated it. I wasn’t trying to make you feel bad, I was trying to get you to actually interact with me and your code.

In my experience, learners remember better when they see the problem with their code themselves rather than me telling them what to do and having them parrot the fix.

Asking questions is good. I recommend asking lots of questions. But it is important to interact with the people who try to help you figure out the answers.

1 Like

No worries, you’re good! I definitely will start making sure I understand the code I write before asking why it doesn’t work! It does make complete sense to me now. :slight_smile:

1 Like

Hi, we know , it is very hard in the beginning to watch every , “;” white space etc.
with time it will become sort of second nature to check your “white space”.
Even today, I spent 2 hours looking for a solution to find that my error was: exactly "a whitespace where it should not be.!
So good luck, hang in there, and happy learning to code!

1 Like