Counting Cards Code

Counting Cards Code
0.0 0

#1

Tell us what’s happening:
Everything works except "Cards Sequence 7, 8, 9 should return “0 Hold”.
Please I need help with and if there’s any problem with my code which is causing it not to work.
Thanks!

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 7:
    case 8:
    case 9:
      break;
        
    case 10:
    case "J":
    case "Q":
    case "K":
    case "A":
      count--;
      break;
  }
  
  
  if (card > 0) {
    return count + " Bet";
  } else {
    return count + " Hold";
  }
  
  return card;
  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc("J"); cc(); cc(5); cc(6);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:


#2

Your approach to part of this challenge is wrong. You appear to be correctly incrementing/decrementing the count variable based on the cards’ values. However, what gets returned should be based on the count value and not the card value which you are currently doing.


#3

okay thanks! But can you show me where within my code? is it with the switch statement?


#4

I will answer your question with two more questions instead of just telling you the answer.

What do you think the purpose of your switch section code is? What does it currently do?


#5

My switch statement acts as something like a conditional to test the cards and to increase/decrease the count


#6

Yes, that is true. So if the switch statement sets the value of count, then another section of your code should be using the count value to make a decision about what to return.


#7

Oooh okay! That means the problem lies within the if/else statement


#8

You seem to have it figured out now.


#9

Thanks for helping me to figure it out.


#10

It now works :grin::+1:


#11

As a challenge to yourself, see if you can replace the Switch statement with if/else if statements to reduce the 22 lines the Switch statement takes up with an if/else if solution which only takes up 4 to 6 lines.


#12

okay, i’ll try that one too


#13

If you get stuck, just post your code and we will give you hints.


#14

hahaha… I think i’m stuck. Only one worked.

var count = 0;

function cc(card) {
  // Only change code below this line
 
 var firstCards = [2, 3, 4, 5,6];
 var secondCards = [7,8,9];
 var thirdCards = [10, "J","Q","K","A"];
  
  if (card == firstCards) {
    count++;
    return count + " Bet";
  } else if (card == secondCards) {
    count = 0;
  } else {
    count--;
    return count + " Hold";
  }
 
  return card;
  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(4); cc(5); cc(6);

#15

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

You really should not have modified the existing if else you had in the original code which used the count to determine the return value. I was only suggesting you replace the Switch statement portion. Your logic is way off when you try to combine the two sections, because they should be doing to different things.

If you are going to use the arrays, then you need to be able to check if a particular card value is in the arrays and depending on which array includes the value should determine to either increment or decrement count.
The issue you are facing is you can not compare a value (string or number) to an array of elements. Check out the indexOf function or includes function which will get you closer to a solution. Your approach is not the way I was thinking, but I believe it has potential to solve the problem with fewer lines of code.