Have an idea?

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

How to I loop a previous answer question using Javascript

Hello,

I am having problems again with looping and a previous answered question.  I can't seem to remember the syntax to use when trying to convert the SSI web code.

I have tried the below but it doesn't work


for (i = 1; i<= 4; i++)
 {
  if (GETVALUE("QS11_" + i) == 1)
    {
      if (SSI_GetValue("QS12_r" + i + "_c2") != 1 && SSI_GetValue("QS12_r" + i + "_c3") != 1 )
      {
              strErrorMessage = "This is a test";    
      }
        
   }  
}

QS11 is the previous answer.  I am trying to replace the code [%QS11_1%] ..[%QS11_4%]



Thank you in advance for your help
asked Mar 28, 2019 by Sandy

1 Answer

+1 vote
The Sawtooth Script functions GetValue and SetValue have no JavaScript equivalents.  You have to use them as Sawtooth Script.  As you probably already know, though, you cannot pass data from JavaScript to Sawtooth Script like this:

var x = 'Q1';
[% GetValue(x) %]


What you instead have to do is restructure the code so that all the Sawtooth Script function can hand off their results in such a way that you can access it in the JavaScript.  Here's a simple example:

var qs11Responses = [[% QS11_1 %], [% QS11_2 %], [% QS11_3 %], [% QS11_4 %]];
for (var i = 1; i <= 4; i++) {
    var qs11Response = qs11Responses[i - 1];
    ...
}
answered Mar 28, 2019 by Zachary Platinum Sawtooth Software, Inc. (134,850 points)
Thank you!
...