3
\$\begingroup\$

I'm calling this function from my html (and in some cases from another function in my JavaScript). These type of switch statements are starting to litter my code. Is there a more elegant solution to these if statements? I wish I could pass a reference to the variables I want to modify. I'm finally asking the question because now I have a situation where if I do it this same way, I'm going to have an ugly switch statement with hundreds of cases which I'm guessing not only looks ugly, but will hurt performance.

function incCriteriaStats(exp){
    switch (exp) {
      case "a":
        numOfA++;
        break;
      case "b":
        numOfB++;
        break;
      case "c":
        numOfC++;
        break;
      case "d":
        numOfD++;
        break;
      case "e":
        numOfE++;
        break;
      case "f":
        numOfF++;
        break;
      case "g":
        numOfG++;
        break;    
    }
|
\$\endgroup\$

4 Answers 4

7
\$\begingroup\$

Why not use an object to store the values for a, b, c, etc, rather than using separate numOfA, numOfB, etc variables:

var obj = { 
   "a": 0,
   "b": 0,
   "c": 0,
   "d": 0,
   "e": 0,
   "f": 0,
   "g": 0
};

This will allow you to do something nice and simple like this:

function incCriteriaStats(exp){
   if (obj[exp] != null)
       obj[exp]++;
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Beat you by just a minute :P \$\endgroup\$
    – Dhaivat Pandya
    Jan 2, 2012 at 17:46
  • \$\begingroup\$ @Dhaivat ok +1 - I found this question so I guess I'm over it :-) \$\endgroup\$ Jan 2, 2012 at 17:58
12
\$\begingroup\$

Use an object, with the keys being the letters, and then you can just do:

function incCriteriaStats(exp) {
        if(exp in obj)
             obj[exp]++;
}
\$\endgroup\$
1
  • \$\begingroup\$ Sorry - looks like your slick answer that beat me by a minute was moved to this black hole. Don't take it too hard, it's happened to me before. \$\endgroup\$ Jan 2, 2012 at 21:34
2
\$\begingroup\$
var obj={};
function incCriteriaStats(exp) {
    if (typeof(obj[exp]=="undefined") {
        obj[exp]=1;
    } else {
        obj[exp]++;
    }
}

The object obj will end up like:

{
    "a": 4,
    "b": 5,
    ...
}
\$\endgroup\$
0
\$\begingroup\$

Obj isn't accessible from global scope, you can only get it by calling incCriteriaStats which increment key exp in obj var and return it

var incCriteriaStats = (function( ) {
        var obj={}
        return function( exp ) { 
          exp ? obj[ exp ] = ( obj[ exp ] || 0 ) +1 : 0;
          return obj;
        }
     })();

incCriteriaStats( "a" )
incCriteriaStats( "a" )
incCriteriaStats( "a" )[ "a" ] // 3
incCriteriaStats( "a" )[ "a" ] // 4
incCriteriaStats( "b" );

var obj = incCriteriaStats() // get the `obj` from inside incCriteriaStatus without modifying it;
obj.a == 4; //true
obj.b == 1; //true
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.