Basic JavaScript - Counting Cards. Hello friends anyone with idea about this challenge I get stuck. Thank you

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

Your code so far

let count = 0;

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


  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/117.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Counting Cards

Link to the challenge:

This looks like the starting code. What have you tried so far?

1 Like

Here is my solution but doest work

let 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+=1;
  break;
  //return count+ "Bet";
  case 10:
  case  'J':
  case 'Q':
  case 'K':
  case 'A':
  count-=1;
  break;
//return count+ "Bet";
  case 7:
  case 8:
  case 9:
count+=0;
}
if(count>0){
  return console.log(count+" "+"Bet");
}
else if(count<=0){
 return console.log(count+" "+"Hold");
}
else{

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

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

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

I tried this solution

let 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+=1;
  break;
  //return count+ "Bet";
  case 10:
  case  'J':
  case 'Q':
  case 'K':
  case 'A':
  count-=1;
  break;
//return count+ "Bet";
  case 7:
  case 8:
  case 9:
count+=0;
}
if(count>0){
  return console.log(count+" "+"Bet");
}
else if(count<=0){
 return console.log(count+" "+"Hold");
}
else{

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

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

It really helps if you use consistent formatting in your code.

What happens when you try to use this code?

Here is your code formatted conventionally

let 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 += 1;
      break;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
      count -= 1;
      break;
    case 7:
    case 8:
    case 9:
      count += 0;
  }
  if (count > 0) {
    return console.log(count + " " + "Bet");
  } else if (count <= 0) {
    return console.log(count + " " + "Hold");
  } else {
    return "Change Me";
  // Only change code above this line
  }
}

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

What does this line do? It doesn’t do what you think it does!

1 Like

Where can I see the edited code?

What edited code?

Yes I want to see the difference between my code and he edited one

Can you not see this full post?

Just to be clear.

console.log returns undefined so that is what you are returning out of the function. Sure your function is logging the correct thing, but it isn’t returning the correct thing.

Can you edit the code to bring the solution? I need help.

No, we cannot write the answer for you. That’s against the rules.

If using console.log() in the return statement is the problem, what might fix the problem?

1 Like