Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

json - Survey with numeric score to each option & custom output based on score

Following is the JSON using surveyjs to build a survey with a question with 4 options and each option has a value (score).

Working example here: https://embed.plnkr.co/20U6sdQpOiTxd5ps83dT/

Here is what is required:

A. I need multiple questions here (25). There is only 1 now.

B. If you answered 4 or more questions 'Unknown' subtract 20 points from total score.

C. Result (Text based) based on score: 75-100 Total Score: Our Service Not Required

50-75 Total Score: You Need An Assessment

50 Total Score: Our Service Is Required

Here is the JSON code:

Survey
    .StylesManager
    .applyTheme("default");

Survey
    .JsonObject
    .metaData
    .addProperty("question", {name: "score:number"});
    
Survey.JsonObject.metaData.addProperty("itemvalue", {name: "score:number"});

var json = {
    questions: [
        {
            "type": "boolean",
            "name": "myboolean",
            "label": "ten",
            "score": 10
        },
        {
            type: "radiogroup",
            name: "myradiogroup",
            title: "Do you have multiple business applications in use by your various departments in the daily operation of the company?",
            colCount: 4,
            choices: [
                {
                  value: "Yes",
                  score: 4
                },
                {
                  value: "No",
                  score: 4
                },
                {
                  value: "Unknown",
                  score: 0
                },
                {
                  value: "NA",
                  score: 0
                }
            ]
        }
    ]
};

window.survey = new Survey.Model(json);

survey
    .onComplete
    .add(function (survey) {
       var totalScore = 0;
       var data = survey.data;
        
       Object.keys(data).forEach(function(qName) {
          var question = survey.getQuestionByName(qName);
          var qValue = data[qName];
          
          if (question.choices) {
            question.choices.forEach(function(choice) {
              if (choice.value === qValue) {
                totalScore += +choice.score;
              }
            });
          } else {
            totalScore += +question.score;
          }
          
        });
        
        document
            .querySelector('#surveyResult')
            .innerHTML = "total score: " + JSON.stringify(totalScore);
    });

$("#surveyElement").Survey({model: survey});
question from:https://stackoverflow.com/questions/65931079/survey-with-numeric-score-to-each-option-custom-output-based-on-score

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...