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.

3
  • what about using switch - case statement? Map with results (array) would be the best as for me, it would be understandable Commented Jun 27, 2012 at 17:48
  • possible duplicate of Alternative to a million IF statements Commented Jun 27, 2012 at 17:53
  • 3
    Maybe instead of 10 variables you just need one - an array. Commented Jun 27, 2012 at 17:53

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Of course this makes more sense to me :) The variables are coming from the post of a form.
I'm fairly new to javascript and I was trying to get the other answer working, but couldn't figure it out. I will work on this later this afternoon.
Ok no problem. Bergi's solution is definitely the most space efficient way to do this. Mine is probably a bit more readable. So whichever makes the most sense for you.
Great good to hear. feel free to accept the answer if it was helpful
@ben336: Please rename your variable. "var" is a reserved word, I even doubt this function would ever work.
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", ...]

5 Comments

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.
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, you might fill your array dynamically of course.
for the var q =etc how come some are "0" and others are just 0
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.
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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.