new_learner
new_learner

Reputation: 53

How to loop through dynamically generated answerkeys?

In MEL how do I loop through dynamically generated answer keys to check if they were checked or not? I have shared a sample code block. I want to loop through answer keys. Here answer keys are being generated dynamically.

<question title="Preferrable Colors" type="4" key="#1">
            <answer nextQuestionKey="END" key=**"#1_1"** position="0">
                <text>Pink</text>
            </answer>
            <answer nextQuestionKey="END" key=**"#1_2"** position="1">
                <text>Red</text>
            </answer>
            <answer nextQuestionKey="END" key=**"#1_3"** position="2">
                <text>Violet</text>
            </answer>
        .......
        <answer nextQuestionKey="END" key=**"#1_10"** position="10">
                <text>Cyan</text>
            </answer>

            <text>Select the colors you prefer </text>

        </question> 

Please suggest me the best approach here.

Thanks

Upvotes: 0

Views: 39

Answers (1)

Inazense
Inazense

Reputation: 1724

You should be used dummy answers in order to add the answers by code in the onEnterAssignment block. After that, you will be able to loop for all the answers using the answer key + client key. This is an example:

<question title="Preferrable Colors" type="4" key="#key">
    <answer nextQuestionKey="END" key="#1_1" position="0" dummyAnswer="true">
    <text>Select the colors you prefer </text>

    <onEnterAssignment>
        colorArray = 
        {
            "1" : "red";
            "2" : "blue";
            "3" : "green";
            "4" : "yellow";
        };
        for (i : colorArray)
        {
            addAnswer($answer:'#key', i, colorArray[i])
        }
    </onEnterAssignment>
</question> 

Upvotes: 0

Related Questions