JavaScript - Card Counting Challenge

JavaScript - Card Counting Challenge
0.0 0

#1

Hi, I have not been able to resolve this, even though I have put or statement for negative count value. I haven’t figured out why the browser doesn’t interpret the negative values since they are not in the intervals specified in the other 2 conditions. Can anyone help?
Here is the code:
var count = 0;

function cc(card) {
  // Only change code below this line
  var answer;
  if(count>0){
    answer=count+1+" Bet";
  }
  else{
    if(count<0){
      answer=count-1+" Hold";
    }
    else{
      answer=count+" Hold";
    }
  }
  if(card>=2 && card<=6){
    count+=1;
  }
  else if(card>=7 && card<=9){
    count+=0;
  }
  else{
    count-=1;
  }
  return answer;
  // 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

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

It is not working because you set answer before you change count. If you put the if(count>0){ ... } just before return answer; it should work.


#3

What are triple backticks? I changed the order as you said, and it only worked now for condition of 7 8 or 9 cards instead of the previous correct 3 conditions.


#4

Hello Ben, I just made a modification on the if count statement and implemented your method, and it worked! Here is the code:
var count = 0;

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

if(card>=2 && card<=6){
count+=1;
}
else if(card>=7 && card<=9){
count+=0;
}
else{
count-=1;
}
var answer;
if(count>0){
answer=count+" Bet";
}
else{
if(count===0){
answer=count+" Hold";
}
else{
answer=count+" Hold";
}
}
return answer;
// 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’);


#5

Glad it works! :thumbsup: