Another Counting Cards Challenge Cry for Help

Another Counting Cards Challenge Cry for Help
0.0 0

#1

So, here’s what I have so far and I’m not quite sure what I’m missing. I didn’t account for the mid cards because they don’t affect your card count - perhaps this is a mistake - I’m not sure what happens when midcards are run through the script - I assume nothing. I keep tweaking it a little here or there but the run test option just returns nada. Any hints/tips/pointing out my logical fallacies would be greatly appreciated. Here’s my code:

var count = 0;

function cc(card) {
  // Only change code below this line
  switch(card){
    case 2 : 
    case 3 : 
    case 4 :
    case 5 : 
    case 6 : answer: ++count; break;
    case '10' : 
    case 'J' :  
    case 'Q' :  
    case 'K' : 
    case 'A' : answer: --count; break;
  }
  if (count>0) return count + " Bet";
  else (count<=0) return count + " hold"; break;
  // 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');

#2

case 6 : answer: ++count; break;
case 'A' : answer: --count; break;

Can you describe what you are trying to do in these lines? What is answer? What are you trying to do with/to it?


#3

also I was wondering if making a case script like what I’ve written below is valid or not, rather than what I have above say write something like this:

case 2 || 3 || 4 || 5 || 6 : count ++; break;

it just takes up so much less space.


#4

that was one of my tweaks, likely a misinterpretatoin from another forum post. Initially I didn’t have it, I suppose I was trying to tell the script what to put out but I can see why it shouldn’t be there, since the function is only to be incrementing or decrementing the count variable not giving an answer just yet.


#5

Question on the pre/post increment/decrement operators:
I’m trying to figure out if my ++ and – should be on the left or right, from what I’m gathering if it is on the left of the variable the ‘new count’ is modified - say a jack already ran through the script and the count went from 0 to +1, then a king went through so the new +1 count would get added to resulting in +2.
If the increment or decrement operators are on the right of the variable they modify the original variable? Going back the the aforementioned example, though your count is +1 from the jack card, the king card would modify the ‘inital count’ value, which was 0, resulting in +1?
Or am I misunderstanding the post increment operator and for this and they are interchangeable in this instance?
Note: I stole this little graphic from c4learn:
image


#6

In most case, pre/post increment doesn’t affect your code. In some rare case, post increment does offer a reduced line of code, but that difference is minimal.

Here’s the difference

let count = 0
console.log(count++) // logs 0
console.log(count) // logs 1

Post-increment evaluates then increments.

let count = 0
console.log(++count) // logs 1

Pre-increment increments then evaluates.

In most cases, by the time you grab the result of post-increment, the result is incremented value.

let count = 0
count++
console.log(count) // logs 1

So, usually the difference doesn’t matter when we just want to increment some value. However, I don’t use post-increment unless I specifically want its behavior because pre-increment matches with my intention more precisely.


#7

I’ve been tweaking a little more so my code now looks like this:

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 (count>0) {
    return count + " Bet";break;}
  else {
    return count + " hold"; break;}
  // Only change code above this line
}

what I want these break lines to do is increment or decrement my count variable if the card meets any of the switch criteria since the last break. E.g. if a Queen is input, when the script hits case ‘Q’ it will decrease my count by 1 then break and begin to move the next card through the operation and adjust the count accordingly. Later this modified count will be called on in the if then operator to display card count and whether to hold or bet.


#8

progress! took out those last 2 unnecessary breaks in the if/then statement and actually got some output, not all conditions are being met to pass the challenge but hey, some green checks are better than none!


#9

Look very carefully at what you return. Remember that in programming things like spaces, punctuation, and capitalization matters.


#10

alrighty, by changing my hold to Hold I have gained 2 additional green checkmarks - we are getting close!


#11

yeah, close is a relative term - I’m not sure why my code won’t work for the following instances:

  • Cards Sequence 10, J, Q, K, A should return -5 Hold
  • Cards Sequence 2, 2, 10 should return 1 Bet
  • Cards Sequence 3, 2, A, 10, K should return -1 Hold

my most recent revision (below) is quite similar to ones before, just stuck to my conviction that cases with no affect on count can be excluded and so I did:

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;
  }
  if (count>0) {
    return count + " Bet";
    } else {
    return count + " Hold";
    }
  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(10); cc('J'); cc('Q'); cc('K'); cc('A');

#12

Oh my gosh, I talked my wife through it and she said that 10 was my common factor and I instantly saw that 10 card was ‘10’ and not 10 … problem solved!


#13

Your wife has an excellent head for programming. I was going to give this same hint. You seem to be doing really well at working through problems. Keep up the good work, happy coding, and give your wife a foot rub!