0

I'm having a problem with getting my returned count to be changed, after I run it through a switch statement. My code looks like following:

var count = 0;
                switch ($(this).last().data('card')) { // Regulating the count to be higher/lower  
                // Hearts
                case 'h2':
                    return count++;
                case 'h3':
                    return count++;
                case 'h4':
                    return count++;
                case 'h5':
                    return count++;
                case 'h6':
                    return count++;
                case 'h10':
                    return count--;
                case 'hj':
                    return count--;
                case 'hq':
                    return count--;
                case 'hk':
                    return count--;
                case 'ha':
                    return count--;

                    // Spades
                case 's2':
                    return count++;
                case 's3':
                    return count++;
                case 's4':
                    return count++;
                case 's5':
                    return count++;
                case 's6':
                    return count++;
                case 's10':
                    return count--;
                case 'sj':
                    return count--;
                case 'sq':
                    return count--;
                case 'sk':
                    return count--;
                case 'sa':
                    return count--;

                    // Diamonds
                case 'd2':
                    return count++;
                case 'd3':
                    return count++;
                case 'd4':
                    return count++;
                case 'd5':
                    return count++;
                case 'd6':
                    return count++;
                case 'd10':
                    return count--;
                case 'dj':
                    return count--;
                case 'dq':
                    return count--;
                case 'dk':
                    return count--;
                case 'da':
                    return count--;

                    // Clubs
                case 'c2':
                    return count++;
                case 'c3':
                    return count++;
                case 'c4':
                    return count++;
                case 'c5':
                    return count++;
                case 'c6':
                    return count++;
                case 'c10':
                    return count--;
                case 'cj':
                    return count--;
                case 'cq':
                    return count--;
                case 'ck':
                    return count--;
                case 'ca':
                    return count--;
            }
        })();

        console.log(count);

When I'm trying to console log it out, it simply outputs 0. It doesn't solve the problem, even if I replace the jQuery with 'h2'.

I hope someone can help me out :-)

Christian Futtrup
  • 162
  • 1
  • 2
  • 14
  • I tried to do that earlier which didn't help it out. I've been trying to find other ressources for the answer, where the way I've set it up at the moment would do the same as using the break; and then returning count in the end of the function. – Christian Futtrup Mar 23 '17 at 01:45
  • Your code contains a huge amount of copy-pasta. Consider refactoring so that you just detect if the second character is a digit or not. One solution is `count += /[hsdc]\d+/.test($(this).data('card')) ? 1 : -1;` – 4castle Mar 23 '17 at 01:49
  • if even using `break` instead of `return`, then that means not any case was caught. So `count` still kept the init value of `0`. – King King Mar 23 '17 at 01:49
  • BTW, do you really understand this `$(this).last()`? That makes no sense, `this` should be just one element. – King King Mar 23 '17 at 01:51
  • @KingKing I took a look at it again and could see the "problem". What I meant was to just insert a class into the jQuery selector. Thanks for noticing. – Christian Futtrup Mar 23 '17 at 03:13

6 Answers6

2

When I'm trying to console log it out, it simply outputs 0.

well you're doing postfix increment:

return count++;

this will return the value of count then increment the value of count.

same goes for this also:

return count--;

this will return the value of count then decrement the value of count.

To overcome the problem simply:

return ++count; 

and also

return --count; 

Postfix and Prefix operators

Community
  • 1
  • 1
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
1

Use ++count and --count instead. These will increment and decrement the value before returning it. Your code returns the current count value of 0 and does not do anything afterwards.

The difference between ++someValue and someValue++.

Community
  • 1
  • 1
tkit
  • 8,082
  • 6
  • 40
  • 71
1

Incremental operators when suffixed will increment the value after.

So... return count++; will return count, and then increment it.

You can prefix the incremental operator to increment before the action happens.

return ++count;

Decremental operators (--count) work the same way.

You can view this fiddle as a sample of usage: https://jsfiddle.net/fjgo4Lwv/

Norman Breau
  • 2,132
  • 16
  • 35
1

No, I'm not answering your question directly. I just feel like I might be able to help you refine your code to something a bit (a lot) easier to work with. It might even help you towards better solutions for the next card manipulations that you might have to perform.

All of that switching... it's was a pain to write, and will be a pain to maintain.

With a bit of string manipulation and a few pre-defined arrays, you can make life a whole deal easier for yourself:

//var cardValue="c10"; //for instance

var suits = ["s", "h", "d", "c"];
var upCards = ["2", "3", "4", "5", "6"];
var downCards = ["10", "j", "q", "k", "a"];

var suit = cardValue.substr(0, 1); //"c"
var card = cardValue.substr(1); //"10"

var count = 0;

if(upCards.indexOf(card) >= 0){
  count++; //postfix operator doesn't do what you think it does, see other answers
}
if(downCards.indexOf(card) >= 0){
  count--;
}
return count;
spender
  • 117,338
  • 33
  • 229
  • 351
0

The ++ postfix operator increments its operand after the operation that contains it. It more concrete terms, if you do

var x = 0;

var y = (function incrementX() {
  return x++;
})()

console.log(y) //prints 0
console.log(x) //prints 1

the ++ actually runs after the return. So the value that gets returned and stored in y is the old value of x.

The ++ prefix operator does what you want. It increments its operand before the rest of the operation. So your code should work if you replace count++ by ++count.

The -- operator behaves the same way.

Pedro Castilho
  • 10,174
  • 2
  • 28
  • 39
0

Switch is not the best choice to use for this case, for is better, but for your learning, you can write switch like this. break or return either one is fine for breakout switch base on your requirement

var count = 0;
var count2 = (function() {
  switch ($(this).last().data('card') || "h2") { // Regulating the count to be higher/lower  
    // Hearts
    case 'h2':
    case 'h3':
    case 'h4':
    case 'h5':
    case 'h6':

    case 's2':
    case 's3':
    case 's4':
    case 's5':
    case 's6':
      return count++;
    case 'h10':
    case 'hj':
    case 'hq':
    case 'hk':
    case 'ha':

      // Spades
    case 's10':
    case 'sj':
    case 'sq':
    case 'sk':
    case 'sa':
      return count--;
  }
})();

console.log(count);
console.log(count2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
YoungLearnsToCoding
  • 427
  • 1
  • 3
  • 10