Basic JavaScript - Counting Cards

Basic JavaScript - Counting Cards
0.0 0

#1

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

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


#3

This post was flagged by the community and is temporarily hidden.


#4

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’);


#5

@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


#6

Hi @fruitman ,

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’);


#7

@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.


#8

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}`;  
}

#9

@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.


#10

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