Solved!Counting Cards Not Working

Solved!Counting Cards Not Working
0

#1

Tell us what’s happening:
I have tried multiple versions of solutions, and while they haven’t worked, I am now wondering if I should somehow traverse an array or possibly linked list to get this done. I’ve tried various if/else if/else statements. I’ve tried switch statements. I’ve also tried the solution, but I haven’t figured out what to change to make it work. Nothing works.

Your code so far


var count = 0;

function cc(card) {
  // Only change code below this line
  switch(card){
    case 2:
      count+1;
      break;
    case 3:
      count+1;
      break;
    case 4:
      count+1;
      break;    
    case 5:
      count+1;
      break;    
    case 6:
      count+1;
      break;
    case '10':
      count-1;
      break;    
    case 'J':
      count-1;
      break;    
    case 'Q':
      count-1;
      break;    
    case 'K':
      count-1;
      break;    
    case 'A':
      count-1;
      break;
  }
  if (count > 0){
    return count + "Bet";
  } else {
    return count + "Hold";
  }
//___________other versions
if (count >0 &&<7){
card = card+1;
return card + "Bet"; //needs count positive sign checker
} else if (count ==0) {
return card; //may need other condition
} else (count <0){
card = card-1;
return card + "Hold";
}

//-------
if (count > 0) {
if (card >0 &&<7){
count = card+1;
count++
return count + "Bet"; //needs count positive sign checker
} else if (card >= 7 && <=9) {
return count; //may need other condition
} else (card > 9){
count = card-1;
count--;
return card + "Hold";
}
return count + "Bet";
} else {
return count + "Hold";
}


  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
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; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/counting-cards/


#2

There is a solution in the hint page.

card >0 &&<7card>0 && card<7 and
else does not accept a boolean expression.
check out js doc on MDN about if control flow .

Red wavy lines under your code means your code is wrong, hover on it to see the error message.

FCC is more like a problem site, always check corresponding doc site before solving problems, this make your knowledage more solid.


#3

for the first version you forgot to assign values back to count variable, ex : count = count - 1, and for the return value it needs a space between the count and the message , ex : 3 Bet


#4

Thanks!
This is the code that ended up working. I had tried doing the count++, count-- from the solution before I even made this forum post. I guess count = count + 1 is what worked.

Here is the code that passed:
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 = count + 1;
break;
case 10:
case ‘J’:
case ‘Q’:
case ‘K’:
case ‘A’:
count = count - 1;
break;
}
if (count > 0){
return count + " Bet";
} else {
return count + " Hold";
}


#5

Thanks for the recommendations.


#6

This really helped! Thank you!