Question T14668
Visible to All Users

Rating Scale - Answer decision

created a year ago

Hi everyone,
I'm working with the 'rating scale' tool, and I have five rating scale questions. I need to return an answer to the user based on the rating scale results. For example, in my case, I'm using four smiley faces for each rating scale question, and the values range from 1 to 4. If 3 out of 5 answers have a value of 1, I need to return one result. If 3 out of 5 answers have a value of 2, I need to return another result, and so on.

How can I create a logic function for this?

Thanks in advance!

Answers approved by surveyjs Support

created a year ago

Hello,
You can create a hidden Expression field and use a custom function to calculate the total value based on supplied Rating values.

Consider this demo: View Demo.

Let me know if you have any further questions.

    Comments (2)
    CS CS
    Curstomer Service a year ago

      Hello Jane,
      Thank you for your response. I'm currently learning about the potential of surveyJS and I find it to be a powerful tool. May I kindly ask if I can implement the solution demonstrated in the "view demo" using the editor tool?

      Thank you in advance!

        Hello,
        Thank you for the update and thank you for considering SurveyJS for your business needs. To apply this solution to a survey creator project, you basically need to copy the code and register a custom function within the survey-core module.

        JavaScript
        import { FunctionFactory } from "survey-core"; function finalRes(params) { let q1 = params[0]; let q2 = params[1]; let q3 = params[2]; // Check if any of q1, q2, or q3 is null or undefined if ( q1 === null || q1 === undefined || q2 === null || q2 === undefined || q3 === null || q3 === undefined ) { return null; // or you can return some other value to indicate an error } const rates = [q1, q2, q3]; // Calculate the result based on rate values const sum = rates.reduce((total, num) => total + num, 0); const average = sum / rates.length; return average; } FunctionFactory.Instance.register("finalRes", finalRes);

        You may need that a SurveyJS Form Library and Survey Creator shares the same survey-core functionality. So, it is sufficient to register a custom function before rendering a survey creator.

        Now, you can define a survey JSON model and use a custom finalRes function in survey expressions.

        JSON
        { logoPosition: "right", pages: [ { name: "page1", elements: [ { type: "rating", name: "question1" }, { type: "rating", name: "question2" }, { type: "rating", name: "question3" }, { type: "expression", name: "totalSum", expression: "finalRes({question1}, {question2}, {question3})" } ] } ] }

        Please refer to the following demo: View CodeSandbox.

        Should you have any further questions, I'd be happy to follow up.