Need help with this challenge, Basic JavaScript: Counting Cards

None of the tests I m performing for this problem is working. Could someone please tell me what I am doing wrong in this code? Am Ieven on a decent path to solving it correctly?

Here’s my solution:

var count = 0;

function cc(card) {
  // Only change code below this line
  if (count > 0){
    console.log(++count);
    return "count Bet";
  }else if(count < 0){
    console.log(--count);
    return "count Hold";
  }else(count===0)
    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');

Link to counting cards

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

First, the logic which determines whether to increase, decrease, or do nothing to count should be based on the card value and not count. Also, the return value will need to be a combination of the current count value concatenated with either " Hold" or " Bet". You are currently returning the string “count Bet” or “count Hold” instead of something like “2 Bet” or “-3 Hold”.

1 Like

You’re posting count to the comment log instead of returning the value and exiting the function.

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 += 1;
      break;
    case 7:
    case 8:
    case 9:
      count += 0;
      break;
    case 10:
    case "J":
    case "Q":
    case "K":
    case "A":
      count -= 1;
}

return count + (count > 0? "Bet" : "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');</p>

So, I followed along to the posted video to help me determine the logic of this problem:
Can someone please help me. I, for the life of me cannot understand the logic he’s using to run the card values at the bottom of the code i.e. : cc(3); . When he runs the bottom values through, if cc(3); increments by count +=1;, why is the card variable 2? Could someone explain this to me like a 4 year old?[Counting Cards, freeCodeCamp Basic Javascript] I don’t care about negative remarks I just want to understand this!!
(https://youtu.be/zgs06k00YIE)

I’m also having difficulty getting this to pass. Any reason why?

I am not sure I understand your question. Can you refer to a specific time in the video and explain in detail what you are not understanding?

You are not passing, because your function returns values like “2Bet” or “-3Hold” instead of “2 Bet” or “-3 Hold”. There is a subtle but important difference. The challenge instructions are quite specific about this detail (see below):

The current count and the player’s decision ( Bet or Hold ) should be separated by a single space.

Around the 6:23 mark.

You got stuck there. I had same problem.

remember cc is the name of the function and we will get only two output. Bet or hold
if count is greater than zero , result is bet

if it is a negative value or 0(-1,-2,-3,-4 ,or 0) result is hold;

I think you feel difficulty to understand this is because you did not understand switch statement
I am not an expert. but i will try to implement this with if statement, then i will try to explain switch

var count=0;

function cc(card){

if(card===2||card===3||card===4||card===5||card===6){
/the count value is zero. when you pass value 2 or 3 4 or 5 or 6 count will be added by one
example
cc is the name of the function
what is the value of count now?
value of count is zero
now i invoke function
cc(2)
what is the value of count now?
one
because this condition is true
/

cc(3)
i invoke the function again
what is the value of count three
how many time this function satisfy this block of code condition as true , it add one by one
count++

}

else{ if(card===10||card===‘J’||card===‘Q’||card===‘K’||card===‘A’){

//same thing here instead of adding it will be subtract
count–

}

else{
//if cc(0) don’t add or subtract, keep the value same
//example
// cc(2) it satisfy the first condition so it added the count"s value by one
now value of count is 1
after that I invoke cc(0)
what is the value of count now:

one
then
if i invoke cc(‘A’)
value of count will be zer

count

}

}

var answer;

if(count;<=0){

//if count lessthan or equal to zero Hold

answer= ‘Hold’

}

else {
if count greater than zero Bet
answer=‘Bet’

}

console.log(count)

return answer;

}

//going to invoke function count (initial value of count is 0) answer
cc(2); count=count+1 =0+1=1 Bet
now value of count =1
cc(2); count=count+1 =1+1=2 Bet
now value of count =2
cc(3) count=count+1 =2+1=3 Bet

now value of count =3

cc(0) count=count+1 =2+1=3 Bet
now value of count same (3) we have not set any arithmetic operation when we invoke function with argument zero
value of count =3
cc(‘A’) count=count-1 =3-1=2 Bet
cc(‘A’) count=count-1 =2-1=1 Bet
cc(‘A’) count=count-1 =1-1=0 Hold
cc(‘A’) count=count-0 =0-1=-1 Hold

in switch statement ‘case’ we use instead of || in if statemen

for example
var value=1

function m(a,b){
if(a<value||b<value){
return value
}

}

in switch statement

var value=1
function m(a,b)

switch(value)

case a<value:
case b<value
return ’ did you understand ’
default :
return slight_smile: