Counting Cards | Ternary Operator

Tell us what’s happening:
Hi, my solution keep failing, I reduced my switch statment to yours.
The only difference what remains is the ternary operator in the adviseMe().
As soon as my function is replaced with your if-elseif then it works.

I can’t see what is wrong, what I keep looking over.

Any idea please?

Your code so far

var count = 0;

function cc(card) {
  // Only change code below this line
  function adviseMe(value) {
    var answer = "";
    value > 0 ? answer += value + " Bet" : answer += value + " Hold";
    return answer;
  }
  
  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;
      
  }
  
  console.log(adviseMe(count));
  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/counting-cards

1 Like

I’ve reduced again. Now it looks this, but as I expected, it fails.

var count = 0;

function cc(card) {
// Only change code below this line
function adviseMe() {
var answer = “”;
count > 0 ? answer += count + " Bet" : answer += count + " Hold";
return answer;
}

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;

}

adviseMe();
// Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display

typo?
var answer = “”;

I made it an empty string, otherwise it won’t let me chain any string onto.

the quotes themselves are backwards?
i replaced them and it worked ok

Good to you, but it doesn’t work here :S I used single quotes, but still the same.

I suspecting my keyboard layout and char encodings then, if you’re right.

Your ternary is not the correct syntax. It should be:

  function adviseMe() {
    var answer = count + (count > 0 ? " Bet" : " Hold");
    return answer;
  }

Also, make sure your cc function returns a value. Currently the function will just return undefined.

1 Like

Thanks, I think it returns the value to the cc(), so just one level above,
therfore it can not reach the surface. Now it works! Big Thanks again!:+1::+1::ok_hand::joy:
The final version is:

var count = 0;

function cc(card) {
// Only change code below this line
function adviseMe() {
  var answer = count + (count > 0 ? " Bet" : " Hold");
  return answer;
}
  
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;

}

return adviseMe();
}

So returning a function execution! =)

You could also get rid of the whole adviseMe function and just do

return count + (count > 0 ? " Bet" : " Hold");

1 Like

Indeed. In the beginning I wanted to keep this apart, but not needed to do so. Thanks! :grinning: