Survey-library: Attribute points to each answer, and calculate the total

Created on 7 Mar 2019  ·  9Comments  ·  Source: surveyjs/survey-library

Are you requesting a feature, reporting a bug or asking a question?

Aking a question

What is the expected behavior?

Add points to each answer (checkbox, radio...) of each question, and calculate total points a the end, as follows :

Question 1
Answer 1 (1point)
Answer 2 (5points)
Answer 3 (10points)
Question 2, question 3...

Score : 55points

Provide the test code and the tested page URL (if applicable)

Tested page URL:

Test code

pages: [
    {
        questions: [
            {
                type: "radiogroup",
                name: "q1",
                isRequired: true,
                title: "xxx",
                choices: [
                    "answer1",
                    "answer2"
                ]
            },
        ]
    },
    {
        questions: [
            {
                type: "radiogroup",
                name: "q1",
                isRequired: true,
                title: "xxxx",
                choices: [
                    "answer1",
                    "answer2"
                ]
            },
        ]
    },


Specify your

  • browser: Chrome
  • browser version: 72
  • surveyjs platform (angular or react or jquery or knockout or vue): Jquery
  • surveyjs version: 1.0.73
question

Most helpful comment

Sure. Here is the basic sample - https://embed.plnkr.co/20U6sdQpOiTxd5ps83dT/

Thak you so much ! I'm looking for since yesterday, but I did not find that ...

All 9 comments

Sure. Here is the basic sample - https://embed.plnkr.co/20U6sdQpOiTxd5ps83dT/

Sure. Here is the basic sample - https://embed.plnkr.co/20U6sdQpOiTxd5ps83dT/

Thak you so much ! I'm looking for since yesterday, but I did not find that ...

I try to use it on several pages and with checkboxes, to accumulate the points on all the pages and to display the total result on the last page. do you have an idea of ​​how to do it?

You can use the onAfterRenderPage event to add page score markup. Here is the sample of the "onAfterRender..." event usage - https://surveyjs.io/Examples/Library/?id=survey-afterrender&platform=jQuery&theme=default

Also you can subscribe the onValueChanged event to update the score.

Thank you for your answer. I don't understand one thing : In my survey, I have this question :

    {
        questions: [
            {
            type: "dropdown",
            name: "firstSelection",
            title: "First selection",
            //visibleIf: "{boolCodeAPE}='true'",
            //isRequired: true,
            choices: [
                {
                    value: "1",
                    score: 0
                },
                {
                    value: "2",
                    score: 0
                },
                {
                    value: "3",
                    score: 0
                },
            ]
        },

        // My 3 sub-selections
        {
            type: "dropdown",
            name: "selection1",
            title: "selection1",
            visibleIf: "{codeAPESelection} = '1'",
            //isRequired: true,
            choices: [
                {
                    value: "a",
                    score: 0
                },
                {
                    value: "b",
                    score: 0
                },
            ]
        },
        {
            type: "dropdown",
            name: "selection2",
            title: "selection2",
            visibleIf: "{codeAPESelection} = '2'",
            //isRequired: true,
            choices: [
                {
                    value: "a",
                    score: 0
                },
                {
                    value: "b",
                    score: 0
                },
                {
                    value: "c",
                    score: 0
                },
            ]
        },
        {
            type: "dropdown",
            name: "selection3",
            title: "selection3",
            visibleIf: "{codeAPESelection} = '3'",
            //isRequired: true,
            choices: [
                {
                    value: "a",
                    score: 0
                },
                {
                    value: "b",
                    score: 0
                },

            ]
        },

    ]
}

If I keep just one "sub selection", it works. If I keep my 3 "sub-selections", I have this error on my console : _Uncaught TypeError: Cannot read property 'indexOf' of null_

This is my code :

.add(function (result) {
        var totalScore = result.getAllQuestions().reduce(function(total, q) {
            console.log('totalScore :' + totalScore);
            if(Array.isArray(q.choices)) {
                //console.log('Array.isArray' + Array.isArray);
                return q.choices.reduce(function(s, c) {
                    console.log('c.value' + c.value);
                    if(q.value.indexOf(c.value) !== -1) {
                        s += c.score;
                        console.log(c.value);
                    }
                    return s;
                }, total);
            }
            return total;
        }, 0);
        console.debug(totalScore);
        var resultsBlock = document.querySelector('#surveyResult');
        resultsBlock.style.display = "block";
        resultsBlock.innerHTML = "Score total : " + totalScore;
 });

Thank you so much for your help !

I tried with just one sub-selection and it works, but I have to have several sub-selections... I also tried to pass sub-selections in next page, but it doesn't work either.

@NicolasLRD if(q.value.indexOf(c.value) !== -1) it works for checkbox, but not for dropdown.
It should be:

function isValueSet(choicesValue, questionValue) {
  if (Array.isArray(questionValue)) {
    return questionValue.indexOf(choicesValue) > -1;
  }
  return choicesValue == questionValue;
}

and if(isValueSet(c.value, q.value))

Thank you,
Andrew

@andrewtelnov thank you for your answer, it's the first time I use SurveyJS (and I must say that I am impressed with the possibilities it offers !).
I understand I have to calculate the score with two functions, one for checkbox, one for dropdowns, but I can't integrate your example in my code.

survey
    .onComplete
    .add(function (result) {
        var totalScore = result.getAllQuestions().reduce(function(total, q) {
            console.log('totalScore :' + totalScore);
            if(Array.isArray(q.choices)) {
                //console.log('Array.isArray' + Array.isArray);
                return q.choices.reduce(function(s, c) {
                    if(q.value.indexOf(c.value) !== -1) {
                        s += c.score;
                    }
                    return s;
                }, total);
            }
            return total;
        }, 0);

    });

I tried this :

function isValueSet(choicesValue, questionValue) {
  if (Array.isArray(questionValue)) {
    return questionValue.indexOf(choicesValue) > -1;
  }
  return choicesValue == questionValue;
}

if(isValueSet(c.value, q.value)){
                 s += c.score;        
 } else {
if(q.value.indexOf(c.value) !== -1) {
    s += c.score;
    }
}

Thank you

@NicolasLRD It should be:

function isValueSet(choicesValue, questionValue) {
  if (Array.isArray(questionValue)) {
    return questionValue.indexOf(choicesValue) > -1;
  }
  return choicesValue == questionValue;
}

if(isValueSet(c.value, q.value)){
    s += c.score;        
 }

Thank you,
Andrew

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmitrykurmanov picture dmitrykurmanov  ·  3Comments

misamura picture misamura  ·  4Comments

spertusatti picture spertusatti  ·  3Comments

dmitrykurmanov picture dmitrykurmanov  ·  3Comments

aslanbeily picture aslanbeily  ·  4Comments