Basic JavaScript - Counting Cards

Hi there! I’ve been reading comments but I haven’t found the answer to my question, so here am I. Could please someone explain why this solution (solution #3) works without even mentioning ‘J’, ‘K’, ‘A’? :thinking:

Your code so far

let count = 0;

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

  if (card <= 6) 
  else if (card >="10")
  return count + (count > 0 ? ' Bet' : ' Hold');

  // Only change code above this line

console.log(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/ Safari/537.36

Challenge: Basic JavaScript - Counting Cards

Link to the challenge:

I don’t know if I like this line. What does that even mean? What if it is a number? What if it is a string?

I would guess that if card is a number, then it coerces '10' to a number and makes the comparison. So, the 10 >= '10' will evaluate to true.

If card is a string, then it makes an alphabetic comparison. Starting with the first character, since "1" comes before any alphabet letter in the unicode sequence, by definition, any alphabetic string will always be “greater than” (come after alphabetically) the "10". So, any of the face cards (which are strings) will evaluate to true here.

Is this good code? OMG, no! This is horrible. This is “cute” code, where you show off some little trick. But good code should not be a puzzle for the next person to read it. And relying on obscure knowledge about unicode and implicit type coercion and relying on that it just kind of happens to work… That is bad code. I would reject this code at work. It’s cure when you’re trying to show off in an algorithm challenge, but not to learn good coding practices.

Don’t assume that the hint answers are any good, or even work. They are not curated or maintained.

1 Like

We have blurred this solution (with [spoiler][/spoiler] tags) so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

Thank you so much for the detailed explanation! It was very informative for me as a beginner to JS and a newbie on this forum.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.