0

I have 10 variables. q1 through q10

my script is as follows:

if (q1 == '1') { q1 = 'Yes'; 
} else if (q1 == '2') { q1 = 'No';
} else { q1 = 'Did Not Answer'; }

I already typed this out for all 10. I am not sure if I can insert another variable into this variable.

I'm trying to do something like this:

var ex = '1';
while (ex < 11) { 
if (q[ex] == '1') { q[ex] = 'Yes'; ex++;
} else if (q[ex] == '0') { q[ex] = 'No'; ex++;
} else { q[ex] = 'Did Not Answer'; ex++ }

Basically I want to eliminate 4 lines of code x 10 variables.

[ex] is a variable inside the variable (to represent q1, determine q1 = Yes, No, or Did Not Answer, then add 1 to q[ex] which would now be q2....

I understand that the [] may not be correct, I just don't know how to explain this in a way that is understandable.

Thank you.

Wex
  • 15,539
  • 10
  • 64
  • 107
Nicholas Decker
  • 454
  • 5
  • 19
  • what about using switch - case statement? Map with results (array) would be the best as for me, it would be understandable – varela Jun 27 '12 at 17:48
  • possible duplicate of [Alternative to a million IF statements](http://stackoverflow.com/questions/10029089/alternative-to-a-million-if-statements) – Felix Kling Jun 27 '12 at 17:53
  • 3
    Maybe instead of 10 variables you just need one - an array. – Pointy Jun 27 '12 at 17:53

2 Answers2

1

See my answer on Alternative to a million IF statements.

The while-loop is of course a good thing, you should really use an array q instead of single variables qN. You might also use a for-loop.

If you want to eleminate the if-statements, you might use an object with indexes (in here an array, which is naturally indexed from "0" to "n-1", does the same job):

var q = ["0", "1", 1, 0, 2, ...];
for (var i=0; i<q.length; i++)
    q[i] = ["No", "Yes"][q[i]] || "Did Not Answer";

// result for q:
["No", "Yes", "Yes", "No", "Did Not Answer", ...]
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • After reading the original (alternative to a mill.....) I wasn't sure of the answer, however this explanation is a little more tailored to what I am doing. Thank you. I believe with the explanation I can adapt this to the 4 or 5 places that I will need these in my script. I appreciate the edit. I was a little lost by the first part. – Nicholas Decker Jun 27 '12 at 18:01
  • Yes, I needed to adapt that answer a bit. You also were asking on using arrays instead of 10 variables. The [...] is a [literal expression for arrays](https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals#Array_literals), you might fill your array dynamically of course. – Bergi Jun 27 '12 at 18:04
  • for the var q =etc how come some are "0" and others are just 0 – Nicholas Decker Jun 29 '12 at 18:20
  • It just means: It will work both for strings, numbers and everything else that will be converted to "0" or "1" (the indizes of the array) when beeing converted to a property name. – Bergi Jul 02 '12 at 18:28
  • I used the other answer because this was a little over my head, however I will learn a little bit more and would believe I will accept your answer. – Nicholas Decker Jul 11 '12 at 18:13
1

This wouldn't limit it down all the way but you could create a function

translate = function(v)
{
    if (v === '1') { 
        v = 'Yes'; 
    } else if (v === '2') { 
        v = 'No';
    } else { 
        v = 'Did Not Answer'; 
    }
  return var
}

Then call translate(q1) etc for each variable. That would move it to ~16 lines rather than 40 and avoid repeating code.

You also could reform your variables as an array of variables and loop through quickly like that, but I don't know where they are all being defined and how that would work for you. EDIT: Bergi has a good example of this. If you can reform your variables like that, that is the best way to go.

Ben McCormick
  • 25,260
  • 12
  • 52
  • 71