Console.log for the Counting Card

Hi everyone,
in regards to the challenge for the Basic JavaScript course (Counting Cards) , the exercise ask to return count + Bet (if count is > than 0) or count + Hold (if count is not > than 0).
Here is the code:

let count = 0;

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

cc(2); cc(3); cc(4); cc(2); cc(2);

My question is, why do we need to log on the console another call of the function and pass another value?

if we assing the result to a variable like:

const result = cc(2); cc(3); cc(4); cc(2); cc(2);

in this case the count will not be shown properly; I would like to understand this part! Thank you!

This will assign the result of cc(2) to the result variable.
Then later it will print it.

So you will see the result of cc(2) later.

how about the end of the code:

cc(2); cc(3); cc(4); cc(2); cc(2);

after passing multiple arguments to the function (2, 4, 2, 2), why do we need to pass another argument (4) and only doing this the code will work properly?

You don’t —need— to. You can.

The code should work whether you log anything or not.

You are calling the function 6 times and the global count value is updated every time. If you like, you can add the console.log() after each call to cc() to see it change over time. What this function is modeling is playing a game where you get handed one card at a time and choose to either “bet” or “hold” based on the cards you have received so far.


I understood now that doing console.log(cc(4)) was like adding another card to the function. So, is this function like a loop? In my example , I only pass values to increment count…and at the end to see the result we just log the function without passing any parameters?

I wouldn’t describe it as a loop, because there isn’t any automated repetition. The function is being manually called multiple times (which is pretty normal) and every call is also causing a global variable to be updated (which is typically a bad idea, but it’s being used illustratively here). This is a situation where if you were writing a similar piece of logic for a functioning program, you would probably use a loop instead.

If you only want to see the count variable, you can log that directly. I wouldn’t recommend calling cc() without any parameters because in that case count will be undefined. I don’t believe that JavaScript will throw an error if you try to compare undefined to a value, but as a rule you want to be careful to be cognizant of what type of value a function parameter is expected to have. (There is even a superset of JavaScript called TypeScript designed to enforce that for better development.)

It is not recommend to call cc() without any parameter?

how can I log count + (count > 0 ? ' Bet' : ' Hold')?

The only way I saw was:

 cc(2); cc(3); cc(4); cc(2); cc(2);

Output is: 5 Bet

If you want to log every returned value you would do


If you only wanted to log the final value, you would do

cc(2); cc(3); cc(4); cc(2); 

The function logic is expecting the argument. Otherwise, the card parameter will be undefined which breaks the logic.

You can also put the return value inside a variable and log that variable before returning it out of the function. But you have to call the function no matter what to see the result of running it.

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