Counting Cards Course Problem

Hi,

I have been recently doing the 1st course of Javascript certificate and I encountered a section where we have to count the value of cards assigned to them.

I managed to the point where we have to assign the count -

let count = 0;

function cc(card) {
  // Only change code below this line
if (card >= 2 && card < 7) {
  count +=  1;
 
}
else if (card >= 7 && card < 10) {
  count = count + 0;
 
}
else if (card === 10 || card == "J" || card == "Q"
|| card == "K" || card == "A") {
  count = count - 1;
 
}

However, I couldn’t solve the next part, but I found the following answer for it -

if (count > 0) {
  return String(count) + " " +  "Bet";
}
else {
  return String(count) + " " +  "Hold";
}
   // Only change code above this line
}

I don’t understand how we have nested “if” in the “else if”?. Semantically, I don’t understand why it’s been done.
I know, the explanation is vague, but I really need help to understand why I am finding the function from that point off? It might not be actually. But it looks so unfamiliar.

Thanks for helping,
Deeti

1 Like

I don’t see a nested if, can you expand on the issue?

I don’t see any nested ifs.
Here’s your code after some nicer formatting:

let count = 0;

function cc(card) {
  // Only change code below this line
  if (card >= 2 && card < 7) {
    count += 1;
  } else if (card >= 7 && card < 10) {
    count = count + 0;
  } else if (card === 10 || card == 'J' || card == 'Q' || card == 'K' || card == 'A') {
    count = count - 1;
  }

  if (count > 0) {
    return String(count) + ' ' + 'Bet';
  } else {
    return String(count) + ' ' + 'Hold';
  }
  // Only change code above this line
}

Actually, it’s not nested. I am talking about the "if " that comes after third “else if.” I am sorry. I know I am not explaining it properly.

This portion:

if (count > 0) {
  return String(count) + " " +  "Bet";
}
else {
  return String(count) + " " +  "Hold";
}
   // Only change code above this line
}

Thanks for replying @ilenia

what issue are you having with it? what do you not understand?

Actually, it’s not nested. I am talking about the "if " that comes after third “else if.” I am sorry. I know I am not explaining it properly.

This portion:

if (count > 0) {
return String(count) + " " + "Bet";
}
else {
return String(count) + " " + "Hold";
}
// Only change code above this line
}

Thanks for replying and make the code readable @Marmiz

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Thanks!! I will remember it now.
So, why did we add if after these line -

function cc(card) {
  // Only change code below this line
  if (card >= 2 && card < 7) {
    count += 1;
  } else if (card >= 7 && card < 10) {
    count = count + 0;
  } else if (card === 10 || card == 'J' || card == 'Q' || card == 'K' || card == 'A') {
    count = count - 1;
  }
if (count > 0) {
    return String(count) + ' ' + 'Bet';
  } else {
    return String(count) + ' ' + 'Hold';
  }
}

Normally, it goes if, else if …, else. But in this code the we have added if after else if. I get it why we have done that. To return the total count. But, the syntax logic is not something I have seen before.

Thank you so much again,

they are two separated piece of logics

remember that the function asks to change count based on card, and after that return a string based on count

2 Likes

I get that part that they are performing separate tasks. But it doesn’t look very intuitive to me.

Thanks again for a prompt replies,

I think I am not able to convey the issue I am facing. Thank you so much :star2:

Like @ilenia said, you have two separate problems you are solving. You can think of if, else if, else as a chain of questions that you are asking, and that chain can be as long as you need to solve your problem. In the one chain of questions you are looking at the card and asking what value is it. If its not in the first if statement it will go on to the if else statement, if its not that then the next if else. If one of those conditions is met you stop going down the chain and move on. You then have another chain of logic with the if then else concerning the count value. Is count > 0? else do that. It wouldn’t really make sense to have these two things in the same chain, because it would break the chain when you check the value of card, so you make it another if/else chain. You could check it and change it in the first if/else chain, but its better to have a separate flow of logic to follow for a different problem to solve.

1 Like

Hi Deeti

In your first if clause, you handle card values from 2 to 6.

Then, in your first else if clause, you handle card values from 7 to 9.

The only remaining card values are 10, ‘J’, ‘Q’, ‘K’ and ‘A’.

So, you could code your if...else statement like this:

if (card >= 2 && card < 7) {
    count += 1;
  } else if (card >= 7 && card < 10) {
    count = count + 0;
  } else  {
    count = count - 1;
  }

Now the else clause is handling cards with value 10, ‘J’, ‘Q’, ‘K’ or ‘A’.

Does that help?

1 Like

Okay. This one looks that it’s the end of one logic properly. So that means in one function we can include multiple logics and they can refer to each other. Is there anything I can read on just to get more comfortable ? When I was writing the function cc It didn’t click me to write one more logic within a function instinctively. I felt so blanked at that point.

Thanks so much for explaining. I really appreciate it :star2:

only practice can help here, keep going!

1 Like

you are right. Thanks for taking out your time :slight_smile:

I completely agree with ilenia.

The more code you write, the more this kind of logic will become intuitive and “automatic” .

The part of the course that you’re working on at the moment is quite long, and it covers a lot of ground.

My advice would be to take it slowly, go back regularly and review anything you don’t feel that you’ve fully grasped, and take plenty of breaks.

You will get there!

True. Also, I think I am more familiar to seeing one logic per function so this one that’s this one seemed a bit off in the moment. You both are right, the more exposure and practice I will get , the more familiarity and comfort will develop.

Thanks @KittyMac and @ilenia :slight_smile:

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