Counting cards(fcc)

Counting cards(fcc)
0
#1

This code is not working,The logic is a bit confusing
/
var count = 0;

function cc(card) {
// Only change code below this line
switch(card){
case 2:
count++;
break;
case 3:
count++;
break;
case 7:
count++;
break;
case ‘K’:
count++;
break;
case ‘A’:
count++;
break;
}
if(cc == 2){
count++;
}
else if(count == 3){
count++;
}
else if(count == 7){
count++;
}

else{
count–;
}

return count;
// Only change code above this line
}

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

0 Likes

#2

if(cc == 2){
count++;
}

I think it should be count not cc in the above if statement condition I have pointed out

0 Likes

#3

Yeah that was a mistake ,but still I am not getting the final output

0 Likes

#4

I see few syntax errors. What should be your final output?

0 Likes

#5

I am getting all 0’s.

0 Likes

#6

I have tried a different code

var count = 0;

function cc(card) {
// Only change code below this line
switch(card){
case 2:
case 3:
count++;
break;
case 7:
count;
break;
case ‘K’:
count–;
break;
case ‘A’:
count–;
break;
}
if(count > 0){
return ‘Bet’;
}
return count;
// Only change code above this line
}

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

0 Likes

#7

Add other cases as well. You missed out some card values in your switch statement.

0 Likes

#8

Also correct your return statement. It should be:
if (count > 0)
return count + ’ Bet’;
else
return count + ’ Hold’;

0 Likes

#9

We have the the function calls as cc(2),cc(3),cc(7),cc(‘K’),cc(‘A’),so what I dont understand is why do we have include all the other switch statements.

0 Likes

#10

Because you need to make your code reusable, not specific for this situation.
And the tests check various other cases.

0 Likes

#11

everything works but the output is
1 Bet

2 Bet

0 Hold

-1 Hold

-2 Hold
which should only return -1,0,1 but not 2 or -2

var count = 0;
var hold = ‘Hold’;
function cc(card) {
// Only change code below this line
if(card == 2){
count++;

}
else if(card === 3){
count++;

}
else if(card === 4){
count++;
}
else if(card === 5){
count++;
}
else if(card === 6 ){
count++;
}
// lower counts

//neutral counts
if(card === 7){
count = 0;
}
else if(card === 8){
count = 0;
}
else if(card === 9){
count = 0;
}

if(card === 10){
count–;
}
else if(card === ‘J’){
count–;
}
else if(card === ‘Q’){
count–;
}
else if(card === ‘K’){
count–;
}
else if(card === ‘A’){
count–;
}
if(count > 0){
return count + ’ ’ +‘Bet’;
}
return count + ’ ’ + hold;
// Only change code above this line
}

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

0 Likes

#12

which should only return -1,0,1 but not 2 or -2

Why do you think so?

You have a global variable, each time the function is called that global variable has its value altered

Also, the correct count values in that card sequence should be 1 2 2 1 0

0 Likes

#13

Don’t do this, you just have to not change the global variable, not reset it to 0. The challenge is specific on this.
Also cc(2); cc(2); cc(2); cc(2); cc(7); the expected result is "4 Bet" instead with your code the result is "0 Hold"

0 Likes

#14

Your older code was working fine. You just have to add cases for the missing card values in your switch statement and in your if condition you have to change your return statement. You have to add the missing card values because your program should be able to compute results for those values as well. The test calls of the function are just for testing purposes.

0 Likes