Stuck at a challenge i solved right, any help, i just need to pass

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

var count = 0;

function cc(card) {
// Only change code below this line
switch(card){
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
    count++;
    break;
  case 10:
  case 'J':
  case 'Q':
  case 'K':
  case 'A':
    count--;
    break;
/*case 7:
  case 8:
  case 9:
    count+=0
    break;*/
}
var holdbet ='Hold';
if(count>0 ){
  holdbet ='Bet';
}
// Only change code above this line
return count+''+holdbet;
}

cc(2); cc(3); cc(7); cc('K'); cc('A');

now I checked the video help, and he made almost the same code I made, then I copied his exact code and used it but I still didn’t pass. why?
thank you.

  **Your browser information:**

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

Challenge: Counting Cards

Link to the challenge:

Put console.log(count+''+holdbet) in there before the return - it should help you understand what the problem is.

works perfectly, it add and subtract 1 when it needs , i check the cards called by the function, it all look right. how ever i found another solution on the forum help and it got me to pass.
but when i used the code that the help video provided, it didn’t pass even if it looks right and give right results on my visual studio.

If you changed your code, you should share it.

I also think that you have a problem with the string you are returning. '' is an empty string. ' ' is something different.

2 Likes

that space in between single quotes has solved it thank you.
as of my previous code, here is it:


var count = 0;

function cc(card) {
// Only change code below this line
switch(card){
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
    count++;
    break;
  case 10:
  case 'J':
  case 'Q':
  case 'K':
  case 'A':
    count--;
    break;
/*case 7:
  case 8:
  case 9:
    count+=0
    break;*/
}
if(count>0){
return count +' Bet';
} else{
return count + ' Hold';
}

// Only change code above this line

}

cc(2); cc(3); cc(7); cc('K'); cc('A');

this has passed the challenge.
it looked little wrong to me specially at the 8th line : card >= ‘A’ && card <= ‘Q’ because the strings Q and A are not number to compare them with < or >, but this still has passed. read below:

var count = 0;
function cc(card) {
  // Only change code below this line
    if (card>1 && card<7){
    count++;
    }else if (card>7 && card<10){
      count=count+0;
    } else if (card == 10 || (card >= 'A' && card <= 'Q')){
      count--;
    }
    if (count > 0){
    return count + " Bet";
    } else {
    return count + " Hold";
    }
}
// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(2); cc(10); cc(0); cc(0);
//console.log(cc('J'));

I’m not a big fan of this solution. What is happening is that card, A, and Q are being converted to their ASCII code values. A has the ASCII value of 65 and Q has the ASCII value of 81.

As written, this code will respond to a lot of invalid values, while the switch version uses strict equality and will not change count based on invalid inputs.

2 Likes

sounds right. thank you.

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