Counting Cards Can't figure it out

Counting Cards Can't figure it out
0

#1

Tell us what’s happening:
I do get only “Cards Sequence 10,J,Q,K,A should return ‘-5 Hold’” right. I was working on a couple hours already just figuring out this one thing. Can you some one explain why i get only one right? and what code should i fix in order to get passed?

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;
  }
  
  var msg = "";
  switch (count) {
    case 5:
      msg = "5 Bet";
    case 1:
      msg = "1 Bet";
    case 0:
      msg = "0 Hold";
    case -1:
      msg = "-1 Hold";
    case -5:
      msg = "-5 Hold";
  }
  return msg;
  // 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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

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


#2

You hardcoded answers in your second switch for only 5 possible results.

What about the case that is not listed in the challenge? What if you will have two cards only provided (eg. 2 and 3) or maybe 10 cards that will count up to 10 etc?

In other words if you have first sequence:
cards Sequence 2, 3, 4, 5, 6 should return '5 Bet'
What result will your code display after first card (2), second (3) etc?

Change your code that it will cover all the possible ways - you can do this with switch - it will take enourmous amount of lines or you can use simple if…else…


#3

Your first switch statement looks to me to calculate count correctly.

That second switch though could get impossibly cumbersome - you would need a case statement for every possible count. That’s a lot of possibilities.

Read the instructions closely

The function will then return a string with the current count and the string Bet if the count is positive, or Hold if the count is zero or negative.

There are really only two possibilities. Count is positive and you return one message or count is not positive and so you return another message.