MíoTuyoSuyo
MíoTuyoSuyo

Reputation: 9

Need help implementing Javascript calculation into Qualtrics

I'm having trouble implementing this code in Qualtrics. Right now, I have created these two embedded data fields in survey flow into the block with the responses I want do calculations on: "Lambda_Block_1" and "Lambda_Mat".

After I ask a series of questions with a matrix table, I do a page break. Then my next question is a place holder/timing window. In this question, I place this Javascript:

Qualtrics.SurveyEngine.addOnload(function()
{
var block_1= [parseInt("${q://QID664/SelectedAnswerRecode/1}"),
  parseInt("${q://QID664/SelectedAnswerRecode/2}"),
  parseInt("${q://QID664/SelectedAnswerRecode/3}"), 
  parseInt("${q://QID664/SelectedAnswerRecode/4}"), 
  parseInt("${q://QID664/SelectedAnswerRecode/5}"), 
  parseInt("${q://QID664/SelectedAnswerRecode/6}"), 
  parseInt("${q://QID664/SelectedAnswerRecode/7}"), 
  parseInt("${q://QID664/SelectedAnswerRecode/8}"), 
  parseInt("${q://QID664/SelectedAnswerRecode/9}"), 
  parseInt("${q://QID664/SelectedAnswerRecode/10}")];

var lambda_mat
lambda_mat= [.7, .9, 1.1, 1.3, 1.5, 1.7, 1.9, 2.1, 2.3];    
lambda_mat.reverse();

  for(i=0;i<lambda_mat.length;i++) {
switchpoints[i] = switchpoints[i] * lambda_mat[i];
}  
var filtered_switchpoint
filtered_switchpoint= switchpoints.filter(0);

var lambda_block_1  
if (switchpoints.allValuesSame() == true || filtered_switchpoint.length              >1){
lambda_block_1=0;
}else{
lambda_block_1= filtered_switchpoint;
}
Qualtrics.SurveyEngine.setEmbeddedData("Lambda_Block_1",lambda_block_1); 
});

I need help running this in Qualtrics. Does anyone have any idea why this wouldn't work? Javascript coding error?

Upvotes: 0

Views: 190

Answers (1)

T. Gibbons
T. Gibbons

Reputation: 5004

You have several syntax errors:

  • Missing parseInt and quotes in first line
  • Missing end of command semi-colon in three places
  • Unmatched }); in last line (but maybe you just didn't include the addOnLoad line at the top).

Also, lambda_mat is an array. For the setEmbeddedData to work you need to convert it to a comma separated string first. Lambda_Block_1 and Lambda_Mat both have to be defined in the survey flow prior to the question with the JavaScript.

You should think about learning to use the browser developer tools debugger.

Upvotes: 0

Related Questions