Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Allow a specific number of questions to be blank, then prompt and terminate if still blank

I'm working on a project where the client would like to allow 5 questions to be blank, then the respondent should be prompted to answer the question and if they don't they should be terminated. Only half of the respondents should be given this option, the other half should be required to answer.

I've worked with the force response/ allow skip logic before, using the code from https://legacy.sawtoothsoftware.com/forum/19763/show-message-but-then-let-respondent-continue as a starting point

I've added a whole number pass in field "skipcount" that is set to 0 on the intro screen. What I'm having trouble with is advancing that field dynamically.
I've tried using the following as custom verification
var flag = $('#customErrorFlag').val();
var numSkips = [% skipcount %];
var m = [% smode %];
var response = SSI_GetValue("[% QuestionName() %]");
if(m!=2 && !response){
    strErrorMessage = "A response to this question is required";
};
if(m==2 && !response && !flag){
    request =true;
    [% SetValue(skipcount,skipcount+1) %];
}
if(m==2 && !response && !flag && numSkips>=4){
    request =true;
    strErrorMessage = "Please provide a response to this question.";
    }

$('#customErrorFlag').val(1);


But the Sawtooth Script activates on page load, not during the verification of the question, so the skipcount is artificially inflated, and the survey gets terminated even when no questions have been skipped.
asked Aug 3 by julietcig (180 points)
edited Aug 3 by julietcig
All Sawtooth Script is server code, so it's not possible to ever run it conditional on JavaScript.  We need to look at situations like this from a different angle to make them work.  Perhaps baking all this logic into the termination skip could work?  So the skip logic would increment the skip counter if a response is blank, then activate the skip if the counter is too high.

I could probably help with such a solution, but I'd want to know what type of questions these are first.
These are single response select questions and a numeric question, but the numeric question is early in the survey and has a static question after it so I can add code on the subsequent question to advance the counter if needed. I tried to just add the code checking for a blank response on each subsequent question, but it broke with the randomizes I have in place.

1 Answer

0 votes
Try adding skips to the relevant questions with skip logic like this:

Begin Unverified Perl
# Settings
my @questions = ('Q1', 'Q2', 'Q3');
my $allowedSkips = 5;

# Run
my $count = 0;
foreach my $question (@questions) {
    if (!ANSWERED($question)) {
        if (++$count > $allowedSkips) {
            return 1;
        }
    }
}
return 0;
End Unverified


Line 3 should include the names of all questions up to the current question that you are placing this script in.  That means that it should be different for each question.
answered Aug 3 by Zachary Platinum Sawtooth Software, Inc. (207,300 points)
...