-
I have some code to show a math problem where the correct answer may be typed in. I have a loop so that the questions should repeat until a certain number have been answered correctly, but the loop is not working; it goes through all possible questions then ends.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @mbeasle2 This is the expected behavior for timeline variables and loop functions. When you add timeline variables to a timeline, the default behavior is to run the timeline once for each entry in the timeline variables array. The loop function runs at the end of this to see if the whole timeline should run again. You can customize how the timeline variables are sampled using the sample parameter. A simple version would be to just sample one trial: var mathQuestionProcedure = {
timeline: [test], // loop the trial
timeline_variables: mathQuestions,
sample: {
type: 'with-replacement',
size: 1
},
loop_function: function(data) {
if (acc < 2) {
return true; // keep going to accuracy criteria
} else {
return false; // end loop
}
}
} The problem with this approach is that you may end up getting the same problem more than once. You could use a custom sampling function to avoid this, though it takes a bit of extra code // creating a global variable to keep track of which problem I'm on
var mathProblemIndex = 0;
var mathQuestionProcedure = {
timeline: [test], // loop the trial
timeline_variables: mathQuestions,
sample: {
type: 'custom',
fn: function(t){
var problemToRun = [mathProblemIndex]; // needs to be an array because the expectation is you are returning list of trials to run.
mathProblemIndex++; // increment for the next time through
return problemToRun;
}
},
loop_function: function(data) {
if (acc < 2) {
return true; // keep going to accuracy criteria
} else {
return false; // end loop
}
}
} |
Beta Was this translation helpful? Give feedback.
Hi @mbeasle2
This is the expected behavior for timeline variables and loop functions. When you add timeline variables to a timeline, the default behavior is to run the timeline once for each entry in the timeline variables array. The loop function runs at the end of this to see if the whole timeline should run again.
You can customize how the timeline variables are sampled using the sample parameter. A simple version would be to just sample one trial: