It can sometimes be difficult to find out why a script is failing. In these cases it can help to use the Response.Write() method to output some variables to the screen. For example, in the script above herewhich failed when trying to set the max question, we could comment out the last line and instead output the max variable to screen:
var max = Math.max(f("q4").values());
Response.Write("Max="+max);
//f("max").set(max);
The text from the Response.Write call will appear at the top of the page before any interview HTML. The result from the script above will look something like this:
Figure 1 - Result of script
If an error appears inside of a loop statement, it often helps to use Response.Write to help identify in which iteration the script fails:
JScript example:
for(var i : int = 0; i<codes.length; i++)
{
Response.Write("i = "+i+"<br>");
< ... >
}JavaScript example:
for(var i = 0; i<codes.length; i++)
{
Response.Write("i = "+i+"<br>");
< ... >
}Other tricks to simplify debugging is to use /* and */ to comment out part of the code to help identify exactly where the error occurs. This can be done as a "binary search": First you comment out ½ of the code in a script. If the error still occurs, you know that it is in the other half. Then you can comment out half of that section – and so on.
It can also be very helpful to use Top line reporting or the "results" tab on a question, the "Edit" functionality under "Survey Data" or doing a data export of the responses with "error" status to look into what answers actually are stored in the question(s) involved in the script code.