Basic JavaScript - Counting Cards

Tell us what’s happening:
i tried with if/else and swicht statement but i can’t get what’s wrong with

Your code so far

let count = 0;

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

  return "Change Me";
  // 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/114.0.0.0 Safari/537.36 OPR/100.0.0.0

Challenge: Basic JavaScript - Counting Cards

Link to the challenge:

Your switch statement works perfectly fine. You can remove the part for 7, 8, and 9, as well, since those numbers do not affect anything.

At the end, where it says

return "Change Me";

You need this to be a string that puts the value of the count variable and then, as the instructions say, return a space and the word Bet if the count is above zero or Hold if it is zero or negative:

i.e. “-3 Hold” or “2 Bet”

Do this with your variable count and then “Hold” or “Bet” based on what the variable count’s current value is.

I did this instead, but i can’t figured out why the number doesn’t display in the console.

let count = 0;

function cc(card) {

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--
  }

 if (card > 0){
   return "Bet"
 } else {
   return "Hold"
 }
  // Only change code above this line
}


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

You are so close, Francis!

You have to include the count variable as part of the return. Remember, it is the number that count currently is, a space, and then the word “Bet” or “Hold”.

2 Likes

btw a cool trick u can do is that u can remove the else statement (but keep the return) and everything would work exactly the same, since return statements stop execution of the function and return back outside of the function call, so the other return would only ever go off if the condition fails!

these kinds of things might feel scary or hard to understand, so don’t feel bad for not writing code like this, or in any other way for that matter, do what you’re comfortable with, and try to improve bit by bit, good luck!

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