Counting Cards - Array String question


I’m just going back through some of the more involved Javascript lessons and I’m curious about how one of the hint answers worked. Here is the code in full:

function cc(card) {
  // Only change code below this line
  var regex = /[JQKA]/;
  if (card > 1 && card < 7){count++;}
  else if (card === 10 || String(card).match(regex)){count--;}

  if (count > 0) return count + " Bet";
  return count + " Hold";

In particular, I’m curious about /[JQKA]/;, what is the significance of the slashes on the outside of the array, and why does that make the challenge pass when without the slashes it does not?

Also curious as to why one of my earlier attempts didn’t work. Code in full:

  var stringCards = ["J", "Q", "K", "A"];
  if (card > 1 && card < 7){count++};
  if (card === 10 || card === "stringCards" ){count--};
  if (count > 0){return count + "Bet"};
  if (count <= 0) {return count + "Hold"};

My curiosity regarding this is I believe I’ve defined strings in the stringCards var, but it shows up as being undefined. Therefore I’m not even sure if my second if statement works, but any guidance on either or both of the topics would be greatly appreciated.

Kind regards,

The / and / represent something in JavaScript as a regular expression. You will learn about them in a later section.

That’s perfect, thanks for letting me know!

You are checking if card is equal to string "stringCards", instead stringCards is a variable with a value of an array, so to check if card is in there you should use an array method, for example includes()

Ah thanks, in truth I think using an array with strings inside is slightly beyond my comprehension at this stage, albeit I did attempt to use the includes() method but without luck.

I am curious as to why a rather more simple attempt didn’t work though, namely this:

 if (card => 2 && card <= 6){count++;}
 else if (card === 10 || card === "J", "Q", "K", "A"){count--;}

 if (count => 1){return count + " Bet";}
 else {return count + " Hold";}

My thinking is that the card will have strict equality to the string, so any of those strings listed there should mean that the condition is fulfilled? But in truth a variety of the other tests haven’t worked and I’m not sure why.

Obviously I’ve solved this challenge by other means long ago but I’m revisiting them with the goal of not using as much help and doing them intuitively, but sadly using the most recent method hasn’t worked, despite me being fairly confident it would!

This doesn’t work as you wish, the commas separate statements, so each one is evaluated independently. If you want to compare them to card you need to compare each individual string.

In this way each comma separated statement is evaluated in sequence, and last one is a string which makes it a truthy value and as a whole the condition of the if statement will always be true

In what way did you use includes()?

Ah ok! I must admit it still isn’t working, but here’s what I’ve tried:

   if (card => 2 && card <= 6){count++;}
 else if (card === 10 || card === "J" || card === "Q" || card === "K" || card === "A"){count--;}

 if (count => 1){return count + " Bet";}
 else {return count + " Hold";}

Any idea why this doesn’t return work for all the conditions of the test?

I must admit I can’t quite remember how I used includes, I do remember that I found it hard to figure out how I would use it in the context. I’ll attempt to remember my attempt though:

var stringCards = ["J", "Q", "K", "A"];


Yeah must admit, whilst not as engrossed as I was when I first tried it I’m not entirely sure how I would use the includes feature to access an array!

I admit I can’t see what’s wrong. Would you mind posting whole code instead of just the function body?

Where have you defined cards? Ignoring wrong syntax, anything can’t work if something is not defined before being used.
includes() is an array method, the syntax is more like Array.includes(Element) and it returns true or false