# Counting Cards undefined error

Tell us what’s happening:

``````
var count = 0;

function cc(card) {
// Only change code below this line
var temp;
var count;
if (card > 1 &&  card < 7){
count ++;
}
else if (card > 6 && card < 10){
count = count;
}
else {
count -- ;
}

if (count > 0){
temp = positive;
}
else if (count = 0){
temp = neutral;
}
else temp = against;

switch(temp){

case positive:
return count + " Bet";
break;

case neutral:
return count + " Hold";
break;

case against:
return count + " Hold";
break;

}

// Only change code above this line
}

// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36`.

This is code thus far, logically it sounds right however my temp is returning undefined which from my exp the entire code resides in a single class hence it shouldnt have prompted this error. I know that there is a correct way of doing this but i;d like pointers on where i am lacking with this code if possible.

temp is defined. However, against, positive, neutral are not.

I assume you meant to make these strings and not variables.

However, doing so overly complicates your code.

``````  if (count > 0){
temp = positive; // why not just return the desired string right here?
}
else if (count = 0){ // is this else if even needed?
temp = neutral;
}
else temp = against; // why not just return the desired string right here?
// You do not need the following Switch statement if you take take my advice above.
switch(temp){

case positive:
return count + " Bet";
break;

case neutral:
return count + " Hold";
break;

case against:
return count + " Hold";
break;

}
``````

This is a non-issue.

Even if you want your code to work in the way you are trying, This is an issue, this is not a comparison, but you are Setting `count` to 0

And the statement is not even needed.

Thank you all for the correction. I have changed the temp variables to string instead and as ieahleen suggests, it seems that else if statement has defaulted all count to 0.

The reason why i wrote that statement was to catch any non negative figures that does not have a value above 1 which on hindsight seems a little redundant after all considering a card count only returns either bet or hold.

Thanks, ill try to revise my code based on the pseudo given.

Thank you for the pointer .

My final working code:

var count = 0;

function cc(card) {

// Only change code below this line

var temp;

if (card > 1 && card < 7){

count ++;

}

else if (card > 6 && card < 10){

count = count;

}

else {

count – ;

}

if (count > 0){

return count + " Bet"

}

else return count + " Hold"

// Only change code above this line

}

// Note: Only the last will display

cc(2); cc(3); cc(7); cc(‘K’); cc(‘A’);

Good job. Do you even need the above else if?

I suppose you meant the below?

Yeap i do need it for the count to increment for all cards between 2 to 6.

I was referring to the else if not your if statement. Then answer is you do not need it at all.

in my opinion i’d still need to count the values of cards between 7 - 9.

i cant just put an else statement as there are cards ( anything above 9 ) which have a negative value.

That is true. I guess what I mean to say is maybe you could just have a single else if which deals with the values above 10 and also the A, J, K, Q cards.

Then, you would not need an else statement.

Writing count = count is redundant.