Advance only on correct answer #1907
-
Hi, I am very new to jsPsych (and Java script as well) and I am trying to implement a task where participants are asked to copy a random three letter word into a text box before being able to continue. I am using the survey-text plugin and am having trouble with retrieving their response and then checking whether it is the same as the three letter stimulus they were displayed. This is the code i have written. for(var i = 0; i< 3 ; i++){
var statement = true
var num = generateRandomStimulus()
var acc = false
var task = {
type: "survey-text",
questions: [{prompt: num}],
preamble: "Re-Type the below characters in the box",
on_finish: function(data) {
var responses = JSON.parse(data.response);
var answer = responses.Q0;
jsPsych.data.addProperties({Participant_answer: answer});
if (jsPsych.data.Participant_answer == num) {
acc = true
statement = false
} else { acc = false};
}
};
var loop_node = {
timeline: [task],
loop_function: function(){
if(acc){
return false;
} else {
return true;
}
}
}
timeline.push(loop_node)
} I keep getting the Uncaught "SyntaxError: Unexpected token u in JSON at position 0" message when i run this. I think this is because data.response is not the correct object to pass into the JSON.parse function. I am unsure how to fix this. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @jhee1610, sorry that validating survey-text responses is a little tricky right now. We're planning to update the survey-* plugins in the near future (later this summer or fall), and when we do that, this will be come a lot easier! In the meantime, I had a look at your code and found a few issues. (I'm assuming you're using jsPsych v6.3.0+, if not then you may need to tweak my solution).
var task = {
type: "survey-text",
// other parameters ...
data: {num: num} // add the current value of 'num' to the data for this trial
}
var answer = data.response.Q0;
on_finish: function(data) {
var answer = data.response.Q0;
data.answer = answer;
// etc. ...
}
if (answer == data.num) { So here's my complete suggestion for your code: for(var i = 0; i< 3 ; i++){
var statement = true
var num = 'abc';
var acc = false
var task = {
type: "survey-text",
questions: [{prompt: num}],
preamble: "Re-Type the below characters in the box",
data: {num: num}, // add the current value of num to the trial data
on_finish: function(data) {
var answer = data.response.Q0; // no need for JSON.parse here
data.answer = answer; // add answer to trial data
// only use the line below if you want to add the answer to the data to all trials - doing this will overwrite the previous answers each time
// jsPsych.data.addProperties({Participant_answer: answer});
if (answer == data.num) {
acc = true
statement = false
} else {
acc = false
};
}
};
var loop_node = {
timeline: [task],
loop_function: function(){
if(acc){
return false;
} else {
return true;
}
}
}
timeline.push(loop_node)
} |
Beta Was this translation helpful? Give feedback.
Hi @jhee1610, sorry that validating survey-text responses is a little tricky right now. We're planning to update the survey-* plugins in the near future (later this summer or fall), and when we do that, this will be come a lot easier!
In the meantime, I had a look at your code and found a few issues. (I'm assuming you're using jsPsych v6.3.0+, if not then you may need to tweak my solution).
num
isn't being saved to the trial data on each iteration of your loop. The reason is that all of the loop code runs before the experiment starts, but the code inside of a function parameter, such as theon_finish
function, doesn't run until during the experiment. So thenum
variable in t…