HELP!~I am stucked at JS counting card challenge

// Only change code below this line
if (card >= 2 && card <= 6){
return count++;
}else if (card==10 || typeof card===“string”){
return count–;
}if (count<=0){
return (count+" Hold");
} else {
return (count+" Bet");
}

return “Change Me”;
// Only change code above this line
}

Can anyone tell me why the result is always 1 less than it should be?
For example, when I tested CC(2)~CC(6), the output only displayed 4 instead of (5, Bet).
Can anyone tell me where did I get wrong?

A walkthrough helps when code produces unexpected results - a good walkthrough is when you pretend to be the computer and simply execute the code line by line variable by variable without explanation or thinking - in fact trying to think or understand during a walkthrough is counterproductive

Here’s your code with line numbers

1. if (card >= 2 && card <= 6) {
2.   return count++;
3. } else if (card==10 || typeof card==="string") {
4.   return count--; 
5. } if (count<=0) {
6.  return (count+" Hold");
7. } else { 
8.  return (count+" Bet");
9. }
10. return "Change Me";

Here’s my walkthrough for the CC(2) through CC(6) test case - this is not code

count=0

CC(2):
1: card=2 -> true
2: count=0, return 0, count=1

CC(3):
1: card=3 -> true
2: count=1, return 1, count=2

CC(4):
1: card=4 -> true
2: count=2, return 2, count=3

CC(5):
1: card=5 -> true
2: count=3, return 3, count=4

CC(6):
1: card=6 -> true
2: count=4, return 4, count=5

I hope this helps you fix your code

1 Like

@ppc was faster than me, but I wrote some comments in it. I hope no bug in my code.

var count = 0;

function cc(card) {
  // Only change code below this line
  
  if (2 <= card && card <= 6) {
    count = count + 1; // shorter: ++count; OR count++;
    console.log("Between 2 and 6.");
  }
  else if (7 <= card && card <= 9) {
    // do nothing with the counter
    console.log("Between 7 and 9.");
  }
  else if (card == 10 ||
           card == 'J' ||
           card == 'Q' ||
           card == 'K' ||
           card == 'A') {
    count = count - 1; // OR shorter: --count; OR count--;
  }
    
  if (count > 0) {
    return count + " Bet";
  }
  else if (count <= 0) { // or just else return count + " Bet";
    return count + " Bet";
  }
  
  // Only change code above this line
}

Thank you very much for your explanation but I am confused with this:
" count=0, return 0, count=1 "
count=1, return 1, count=2

Does that mean “The return statement stops the execution of a function and returns a value from that function.”?

Btw, I removed return from line 2 and 4, which made the code worked!~

Thank you for the comments ~It helps!~

That’s just my walkthrough notes for line 2 of your code - think of it as a comment representing the state of the program when line 2 is executed for CC(2)

// CC(2)
2.   return count++;
// 2. count=0, return 0, count=1

When line 2 runs the program uses the value of count which is 0 for the return value of the function - then count is incremented and becomes 1 - since count is global its value remains 1 for the next call of CC(3)

I got it now! Thank you very much!!

I’m going to add one of the mistakes I was making with this challenge is rather than using proper incrementation:

var++;
var--;

I was writing:

var = var + 1;
var = var - 1;

By working through the challenge I learned that this was a bad style of code and I changed to proper incrementation and the challenge was solved.

it is not a mistake or bad style in my book - it’s only more verbose

The bad styling was causing the code to be unable to do it’s job properly, this might of been due to it’s use in the switch, I’m not sure, if I have time I’ll go back and investigate further but for now ++ and – incrementation will always save the day. And I can just write a for loop if I need to increment by 100 and what-not. This javascript stuff is getting fun - at first I felt a bit intimidated but now I’m enjoying it :slight_smile: