Basic JavaScript - Counting Cards

Tell us what’s happening:
Here is the solution using if-else

Your code so far


var count = 0;

function cc(card) {
  // Only change code below this line
  if(card==2 || card==3 || card==4 || card==5 ||card==6){
   count++;
  }
  else if (card == 7 || card == 8 || card == 9) {
      count;
  }
  else if (card==10 || card=="J" || card=="Q" || card=="K" || card=="A"){
    count--;
  }
  
  
  if(count<=0){
     return (count + " Hold");
  }
  else{
      return (count + " Bet");
  }
  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(4); cc(5); cc(6);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/counting-cards

2 Likes

Hi @Mehreen

Nice solution

Since that is a working solution could you be kind enough to hide that between spoiler tags?

[spoiler]
Any text between spoiler tags will be blurred
[/spoiler]

Someone that is still working on the challenge might not want to see it.

Thank you

Just to share another solution using Switch, not sure if it’s a good way though…

var count = 0;

function cc(card) {
// Only change code below this line
var bet = “”;
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count = count + 1;
break;
case 10:
case ‘J’:
case ‘Q’:
case ‘K’:
case ‘A’:
count = count - 1;
break;
default:
break;
}

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

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc(‘K’); cc(‘A’);

@lootster
I think it’s good to know how to solve this challenge using both the else if and the switch statements. If I may ask, what was the purpose of the line 5 variable declaration in your solution?
var bet = “”;

count = count + 1; should just be count++;.

In your switch statement, case 7, case 8 and case 9 are missing. I am also a newbie, so I can’t explain how you passed the challenge without including these 3 cases.

The last 3 lines of your switch statement should be
default:
default statement; // missing in your code
break;

In addition, I may be wrong but I don’t think the default case was necessary for this challenge.

Cheers

1 Like

Hi @anon46395391 ,

Thanks for pointing out!

var bet = “”;

Forgot to delete this line, was a mistake.

count = count + 1; should just be count++;.

You’re right! Seems to make the code neater although both methods works.

In your switch statement, case 7, case 8 and case 9 are missing. I am also a newbie, so I can’t explain how you passed the challenge without including these 3 cases.

Above 3 scenarios wouldn’t increment the count, so I have omitted them. Any thoughts?

The last 3 lines of your switch statement should be
default:
default statement; // missing in your code
break;
In addition, I may be wrong but I don’t think the default case was necessary for this challenge.

Yes! It still works without using “default”.

Have amended my code as shown below…

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) {
return (count + " Bet");
}
else {
return (count + " Hold");
}

// Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc(‘K’); cc(‘A’);

1 Like

@lootster
You’re welcome, mate. We should help out each other to have any chance of surviving this camp. The code looks good now. I am also confused by 3 cases you omitted, but you’re right. These 3 cases do not affect the global count variable.

Here is something I just noticed (by pure chance):
Both return statements are correct.
return (count + " Bet"); //with brackets
and
return count + " Bet"; //without brackets

But, I think the brackets improve readability.
Cheers.

1 Like

Congrats on finding a simple, working solution. There are a couple ways that you could make your code a little more concise and easier to read, by using Array.prototype.includes and template literals. These language features are available in most browsers, but not in IE 11.

let count = 0;

const lowCards = [2,3,4,5,6];
const highCards = [10,'J','Q','K','A'];

function cc(card) {

  if(lowCards.includes(card)) {
    count++;
  } else if(highCards.includes(card) {
    count--;
  }

  const action = count > 0 ? 'Bet' : 'Hold';

  return `${count} ${action}`;  
}
4 Likes

@mcondon
Thanks for composing this beautiful, but really advanced code, and I can’t wait to reach this level too. But, I haven’t reached the ES6 challenges yet. I am in the ES5 dark ages and still have a long way to go before I can even begin to understand your code.
Cheers.

awesome code :slight_smile: just the else if … need one more " ) "

Why I can’t do just like this? Screenshot_68

Hi @nastya

I think you need to read the instructions more carefully.

You are to change count based on the value of the card. You do not set count to the value of the card.
For example if card is 2,3,4,5, or 6 then you add one to count
You change count similarly for the other card values. See the chart in the challenge instructions.

Then you choose a string based on the current value of count. Bet or Hold

And then return current value of count and your string

hi ,i 'm using the solution with the switch function , how can i test this function, i used to try console.log(cc(2, 3, 4, 5, 6 )); but it gives me a different result , 1 Bet instead of 5 bet , , i don’t figure out how is this, have you got an idea about this , maybe i’m not using the console.log correctly with the function ??!!!`var count = 0;

type var count = 0;

function cc(card) {
  
      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){
        return count + " Bet";
      } else {
        return count + " Hold";
      }
     
    }
  
console.log(cc(2, 3, 4, 5, 6 ));

Check the tests, and the original function call
For each set of cards the function is called on each card
console.log(cc(2),cc(3),cc(4),cc(5),cc(6))

1 Bet,2 Bet,3 Bet,4 Bet,5 Bet this the result of your console.log, it’s not the same shown on the solution section in the FCC. I don’t undesrtand , i am quite stuck

excuse me i’m newbie in javascript, so one another idiot question, is there any difference between the console.log in FCC and the one of another browser , i’ve paste the above code in chrome and it gave the result 5 Bet neither in the console.log of FCC which like i’ve said before gave me a value of 1 Bet for the same fonction , i don’t understand , and is there any difference between calling a function(exp: cc() + press enter) and (calling console.log(cc()).!!! sorry for my english , im trying to perform it

Every function returns something. If you write a function inside a console.log() statement that something will be added to your console. The console in FCC has some limitations, so you better open the browser console when you run the tests to see the things that your function returns.

The thing you need is the result from the last card, which gives the correct answer you were expecting.
You can wrap only the last function call in the console.log() statement if you need to avoid confusion

1 Like

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

Hey @marouane1987 I don’t want to create another thread with the same question… Were you able to find out how to get only the final result (5 Bet) instead? I am also new to JavaScript, and have tried a lot of things and nothing has worked so far. Thanks!

You have 5 function calls, all functions return something. You are interested in the value of the last one. You can wrap only the last function call in a console.log() statement