Iterate through questions to filter answers

4 years 8 months ago #88810 by garf
garf created the topic: Iterate through questions to filter answers
Hi all,

I've been playing with LimeSurvey 2 of late and I have to say I'm totally impressed!

I've run into a situation though and I'm wondering if anyone can help?

To keep it simple:

I have 2 ranking type questions(the real survey has 42).
I have 4 pools of possible answers for each ranking, say A,B,C,D.
Each answer code begins with one of these letters, so, A1, B1, C1, D1 - A2, B2, C2, D2
I only want to score items ranked in 1'st and 2'nd place.
Whatever pool letter is Ranked 1 will get 5 points, 2nd place 3 - 4th and 5th are irrelevant.
At the end of the survey, I want to summarize scores/totals across the questions pools for A, B, C D .

-- -- -- -- Q1 Q2
Rank 1 -- A A
Rank 2 -- C B

-- -- -- -- --Q1-- -- Q2
Rank 1 -- A=5 -- A=5
Rank 2 -- C=3 -- B=3

= A=10, B=3, C=3, D=0

I tried using something like this to give me some totals

if(substr(Q1_1, 0, 1) == "A", ATotal=ATotal + 5, if(substr(Q1_1, 0, 1) == "B", BTotal=BTotal + 5, if(substr(Q1_1, 0, 1) == "C", CTotal=CTotal + 5, if(substr(Q1_1, 0, 1) == "D", DTotal=DTotal + 5, 0))))

if(substr(Q1_2, 0, 1) == "A", ATotal=ATotal + 3, if(substr(Q1_2, 0, 1) == "B", BTotal=BTotal + 3, if(substr(Q1_2, 0, 1) == "C", CTotal=CTotal + 3, if(substr(Q1_2, 0, 1) == "D", DTotal=DTotal + 3, 0))))

..but I get scores like 39 and 43 etc... So it seems like the code keeps getting triggered when there is activity in the survey.

Has anyone seen this before or have some suggestions how to handle this?

The problem is when you have 42 answers x 4, the equation gets really long. Is there a way to filter or iterate through the question perhaps with a scripted loop or something?

Thanks for any guidance in advance!

G

4 years 8 months ago #88826
DenisChenu replied the topic: Iterate through questions to filter answers
Just an idea,

After each ranking (the number is X, example for first and A), add 4 equation question (attribute : numeric):
```CODE : SUMA01
Text : {(if(Q1_1=="A1",5,if(Q1_2=="A1",4,0))}
CODE : SUMB01
Text : {(if(Q1_1=="B1",5,if(Q1_2=="B1",4,0))}
CODE : SUMC01
Text : {(if(Q1_1=="C1",5,if(Q1_2=="C1",4,0))}
CODE : SUMD01
Text : {(if(Q1_1=="D1",5,if(Q1_2=="D1",4,0))}```
For the second
```CODE : SUMA02
Text : {(if(Q2_1=="A2",5,if(Q2_2=="A2",4,0))}
CODE : SUMB02
Text : {(if(Q2_1=="B2",5,if(Q2_2=="B2",4,0))}
CODE : SUMC02
Text : {(if(Q1_1=="C2",5,if(Q1_2=="C2",4,0))}
CODE : SUMD02
Text : {(if(Q1_1=="D2",5,if(Q1_2=="D2",4,0))}```
(each equation are the same, just change X.

At the end of the survey:
CODE : TOTALA
Text : {sum(SUMA01,SUMA02,SUMA03 .... )}

It's more easy to do, just a long way, but you already adding 42 ranking question.

Denis

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

4 years 8 months ago #88906 by garf
garf replied the topic: Iterate through questions to filter answers
Thanks Denis for the great idea! I have to admit, I didn't even think to break it down that way.

In the interest of expanding my knowledge, would you or someone else here on the forum know, if I did want to run a script after the last question, say something like below; given quesiton code is A1_1. A2_2 ..etc.

Is it possible to access the questions this way?
```

<script type="text/javascript">

AT=0;
BT=0;
CT=0;
DT=0;
i=1;

while (i<43)
{
str= "{Q" + i + "_1}"
switch(str.substring(0,1))
{
case "A":
AT=AT+5;
break;

case "B":
BT=BT+5;
break;

case "C":
CT=CT+5;
break;

case "D":
DT=DT+5;
break;

}

i++;
}

</script>

```

Garf

4 years 8 months ago #88931 by tpartner
tpartner replied the topic: Iterate through questions to filter answers
It looks to me like the loop should work, but I think the question codes should be Q1_1, Q1_2...

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

4 years 8 months ago #88970
DenisChenu replied the topic: Iterate through questions to filter answers
`str= "{Q" + i + "_1}"`
Thinks EM manager don't work here.

EM mangaer look at
`Q" + i + "`
vars, and this var donesn't exist.

Denis

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

4 years 8 months ago #88972
DenisChenu replied the topic: Iterate through questions to filter answers
```var questionArray=new Array("{Q1_1","Q1_2","Q1_3" ... );
len=questionArray.length;
for (var i = 0; i < len; i++) {
{
case "A":
AT=AT+5;
break;

case "B":
BT=BT+5;
break;

case "C":
CT=CT+5;
break;

case "D":
DT=DT+5;
break;

}
}```
Don't look at the number of {/} maybe some error.

Denis

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

4 years 8 months ago #89010 by garf
garf replied the topic: Iterate through questions to filter answers
Thanks Denis! That solved the mystery for me... I was having a problem with version 2 and direct script in the question with {INSERTANS:SQGA}. It kept filling up my script with html etc.

The {qcode} works perfectly in script and the array solution makes perfect sense.

Garf