Card Counting Lesson

A

  **Your code so far as far as I can tell the my solution should work, but when I run the tests it says I fail on every count. It must be something basic I'm just not seeing. Do you have an insight into where my code has  gone wrong?

let count = 0;

function cc(card) {
// Only change code below this line
switch (cc) {
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;
}
var display = "Hold"
if (count > 0) {
display = "Bet"
}
return count + "" + display;
// 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/100.0.4896.88 Safari/537.36

Challenge: Counting Cards

Link to the challenge:

I see three problems in your code (4 if you count the same issue in two places).

At this point you should have the debugging skills to find the issues. Even a basic step of logging out the data in and out of the function should point you to the problems. That is always my first step.

One of the most important skills a developer has is debugging. And debugging means being a good detective.

Consider this code:

let count = 0;

function cc(card) {
// Only change code below this line
  console.log('switching on value', cc)
  switch (cc) {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      console.log('before increment, count', count)
      count = +1;
      console.log('after increment, count', count)
      break;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
      console.log('before decrement, count', count)
      count = -1;
      console.log('after decrement, count', count)
      break;
  }
  var display = "Hold"
  if (count > 0) {
    display = "Bet"
  }
  return count + "" + display;
// Only change code above this line
}

console.log(cc(2))
console.log(cc(3))
console.log(cc(4))
console.log(cc('A'))
console.log(cc('K'))
console.log(cc('Q'))

Run that and see the results and see if you can’t figure out what the issue is. Check back if you get stuck. All the information you need to fix this will be revealed in those log statements, as you fix things.

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