Have an idea?

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

Java script validations

Hey guys,

I am trying to validate a question. Part of the code I am using is below;

var i=0;
for (i = 0; i < 50; i++) {
    if [% ListHasParentMember(q27ConList,i+1) %]=1 {
        Arr_RowList.push(i+1);}
    else if [% ListHasParentMember(q27ConList,i+1) %]=0    {
        Arr_RowList.push(0);}
    }

 I'm keep on getting the error saying that the symbol "i" doesn't exist. My assumption is that the Global Javascript variable I assigned as"i" does not get recognized inside the perl "ListHasParentMember" function.

Please let me know if anyone knows a way around this
asked Aug 22 by anupa (120 points)

1 Answer

0 votes
The key to keep in mind is that Sawtooth Script is always evaluated before JavaScript.  Your code creates a variable "i" in the JS and tries to pass that variable to Sawtooth Script, but the Sawtooth Script is already done by that point.  So what you need to do is rework the code so that all Sawtooth Script is done independently of the JS.

Fortunately, the Sawtooth Script function "ListValuesArray" was made exactly for situations like yours.  This function creates a JS array with all the info you want, then the JS can just use that array rather than using Sawtooth Script with "i."

var arr = [% ListValuesArray(q27ConList) %];
for (var i = 1; i <= 50; i++) {
    var inConstList = arr.indexOf(i) != -1;
    if (inConstList) {
        Arr_RowList.push(i);
    }
    else {
        Arr_RowList.push(0);
    }
}
answered Aug 23 by Zachary Platinum Sawtooth Software, Inc. (63,475 points)
...