Basic JavaScript - Counting Cards - the log prints bet/hold after EACH card?

Tell us what’s happening:

So I’m trying to figure out how to get the log to stop printing ’ bet/hold’ after EACH card. ‘break’ should make it move to the next card, but then how does it ever get to the if/else statement that prints the card count and bet/hold recommendation? Each iteration of the function includes the if/else statement that prints the count and recommendation - it makes sense that it would print something after EACH card. How would you get the function to perform only half the function until the very last use?
(I’m probably overthinking this.)

Your code so far


var count = 0;

function cc(card) {
// Only change code below this line
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;
}

if (count > 0) {
  console.log(count + " Bet");
} else {
  console.log(count + " Hold");
}

// Only change code above this line
}

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/87.0.4280.141 Safari/537.36.

Challenge: Counting Cards

Link to the challenge:

You are so close.

Remember that functions need to return something.

Right now yours does not.

1 Like

right, so I tried deleting the console.log commands in the if/else statement at the bottom, replaced them with return, and it worked. Seems like a cop-out. I’d still like to see the recommendation and the count in the log, y’know?

right, so I tried deleting the console.log commands in the if/else statement at the bottom, replaced them with return, and it worked. Seems like a cop-out. I’d still like to see the recommendation and the count in the log, y’know? Can i do return console.log? That would still print multiple instances of my count/recommendation though.

Thanks for your response btw

If you want to test your code, you can test it with the test cases provided for you.

console.log(cc(2))

Prints 1 Bet

FCC gives you a variety of test cases that you can use console.log on.

is console.log purely a test kinda thing? It’s not necessary to print stuff to the bottom?

Yes it is.

You usually use it as a debugging tool to see what your code is actually doing and giving you the results you are looking for.

1 Like

ohh okay, thank you!

One last question - so say someone wants to use this little card-counting program - I’d have to do something in HTML/CSS, make somewhere they can pop their cards in, have this little JS thingo running in the background, and then display the function’s answer to the person reading the webpage, yeah? Actually printing the recommendation so it can be seen, is that more of a HTML/CSS consideration?

I apologise if this is out of the scope of the question or if I’m getting too far ahead of myself

You could do that with javascript.

Remember that html and css are for content and style.

Javascript is the programming language where you can create functionality and interactive moments on your website.

Once you dive deeper into the javascript curriculum and start working on this project idea than you will start to understand how all three of these languages work together to make this idea come to life.

And then if you get stuck on this project than you can create a new topic and ask your questions there.

Hope that helps!

1 Like

Thanks, I’ll keep on truckin’ then!

Appreciate your help!!