Proper output but the code does not pass the test for Counting Cards

I am receiving the proper outputs but I am not passing the tests.

Your code so far


var count = "0";

function cc(card) {
// Only change code below this line
switch (card) 
{

case 2:
count++;
break;

case 3:
count++;
break;

case 4:
count++;
break;

case 5:
count++;
break;

case 6:
count++;
break;

case 7:
count+=0;
break;

case 8:
count+=0;
break;

case 9:
count+=0;
break;

case 10:
count--;
break;

case 'J':
count--;
break;

case 'Q':
count--;
break;

case 'K':
count--;
break;

case 'A':
count--;
break;

}

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

else (count <= 0)
return 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/86.0.4240.183 Safari/537.36.

Challenge: Counting Cards

Link to the challenge:

Ok I found out what I was doing incorrectly… I got rid of the console.log() bit and that passed the test. I was wondering if someone can tell if the original code is still correct?

var count = "0";

function cc(card) {
  // Only change code below this line
switch (card)
{
case 2:
count++;
break;

case 3:
count++;
break;

case 4:
count++;
break;

case 5:
count++;
break;

case 6:
count++;
break;

case 10:
count--;
break;

case 'J':
count--;
break;

case 'Q':
count--;
break;

case 'K':
count--;
break;

case 'A':
count--;
break;


}

if (count > 0){
  return count + " bet";}

  else (count <= 0)
 return count + " hold";
}

Your original code is not correct. Console.log only prints out the values to a log so you can see them. Returning a console.log of a value does not actually return the value for you.

1 Like

Hi. after else you don’t need to define a condition, you just need and open and close curly brace .

if (count > 0){
  return count + " bet";
} else{
 return count + " hold";
}
2 Likes

Ok i didnt know that it still did not return a value that way …thanks a bunch.

ok great, i knew there was another way for cleanser code. Thanks!

1 Like

Just to be clear, console.log() returns undefined (as per the specs).

console.log("test");
// test
// undefined
function consoleReturn() {
  return console.log("test");
}

consoleReturn();
// test
// undefined
function returnValue() {
  return "test";
}

returnValue();
// "test"
1 Like

Thanks for the tip. I posed the question because in the test output I could see the logic of the code working. So I was confused as to why I was getting it wrong.

I just wanted to mention it because the undefined returned from console.log() sometimes confuses people.

But I can see how the output might be confusing. If you want to log the return value from a function you can wrap the call to the function inside a log.

console.log(someFunction(someArgument));
// someReturnValue

Although in this case because it is testing for a sequence of calls you would need to replicate that to see the correct last value.

console.log(cc(2))
console.log(cc(3))
console.log(cc(4))
console.log(cc(5))
console.log(cc(6))
// last call would return 5 Bet
1 Like

I see more clearly now. I appreciate you pointing out to me the deeper nature of “console logging.”

1 Like

other possible reasons of failure:

  • output completely lowercase instead of capitalized as required

  • why have you changed the starting value of count? I mean, it’s JavaScript so it’s not like it doesn’t work, but it was a number originally, not a string

° I did capitalize “Hold” and " Bet" in my final solution.

° I changed the value to a string because the instructions asked for the output to be a string.

it works anyway, because it’s JavaScript and it’s losely typed, but you are adding to a string, that’s confusing. You shouldn’t change a number that needs to be added or substracted to a string