So I'm working on a test that will repeatedly loop through a subset of trials until a specific amount of correct responses are made in a row. Now I realize that a counter function is what has to be used but I am totally incapable of figuring out how and where to write it in.
To clarify a little more, the participant will start the test, will be given a block in which 'categorize' trials require a response to be made. If responses on the trials reach a total of 8 correct then the block ends and a new block begins. Now I've managed to group the trials in blocks and loop the block in the following fashion:
The problem is that I can only get the experiment to somewhat function by setting the trial_count_correct to = 8. No matter how i include a counter function the only values I am able to pass on into var trial_count_correct is either 0 or NaN. I've tried linking it to the standard function getSubjectData(); by including a return that counts correct responses. The result is constantly NaN. This is weird because the standard function works in the debrief_block and shows the correct accuracy %.
I have spent hours now trying to make alternate functions editing the functions, adding on_finish statements all to no avail. How would you suggest to solve this? I've tried reading questions others have had with similar problems but being a novice I'm finding it difficult to implement the solutions to my code.
I also wonder if I can use the jsPsych.finishTrial to end the block before the loop function has to loop through the whole timeline.
var test_stimuli = {
type: 'categorize',
stimulus: '<p>A1</p>',
key_answer: 70,
choices: [70,74],
data: { response:'ll'},
correct_text: '<p>Correct!</p>',
incorrect_text: '<p>Incorrect!</p>',
is_html: true,
on_finish: function (trial_data){
var correct = (trial_data.key_press == 70);
jsPsych.data.addDataToLastTrial({correct: correct});
}
}
var test_stimuli2 = {
type: 'categorize',
stimulus: '<p>A2</p>',
key_answer: 70,
choices: [70,74],
data: { response:'ll'},
correct_text: '<p>Correct!</p>',
incorrect_text: '<p>Incorrect!</p>',
is_html: true,
on_finish: function (trial_data){
var correct = (trial_data.key_press == 70);
jsPsych.data.addDataToLastTrial({correct: correct});
}
}
var test_stimuli3 = {
type: 'categorize',
stimulus: '<p>B1</p>',
key_answer: 70,
choices: [70,74],
data: { response:'ll'},
correct_text: '<p>Correct!</p>',
incorrect_text: '<p>Incorrect!</p>',
is_html: true,
on_finish: function (trial_data){
var correct = (trial_data.key_press == 70);
jsPsych.data.addDataToLastTrial({correct: correct});
}
}
var test_stimuli4 = {
type: 'categorize',
stimulus: '<p>B2</p>',
key_answer: 70,
choices: [70,74],
data: { response:'ll'},
correct_text: '<p>Correct!</p>',
incorrect_text: '<p>Incorrect!</p>',
is_html: true,
on_finish: function (trial_data){
var correct = (trial_data.key_press == 70);
jsPsych.data.addDataToLastTrial({correct: correct});
}
}
var post_trial_gap = function() {
return Math.floor( Math.random() * 1500 ) + 750;
}
var test_block = {
timeline: [test_stimuli, test_stimuli2, test_stimuli3, test_stimuli4],
randomize_order: true,
loop_function: function(data){
if(trial_count_correct < 8){
return true;
} else {
return false;
}
}
};
var testBlockArray = [test_block, test_block];
/* define debrief block */
var trial_count_correct = 8
function getSubjectData() {
var trials = jsPsych.data.getTrialsOfType('categorize');
var sum_rt = [0];
var correct_trial_count = 0;
var correct_rt_count = [0];
for (var i = 0; i < trials.length; i++) {
if (trials[i].correct == true) {
correct_trial_count++;
if(trials[i].rt > -1){
sum_rt += trials[i].rt;
correct_rt_count++;
}
}
}
return {
rt: Math.floor(sum_rt / correct_rt_count),
accuracy: Math.floor(correct_trial_count / trials.length * 100)
}
}
var debrief_block = {
type: "text",
text: function() {
var subject_data = getSubjectData();
return "<p>You responded correctly on "+subject_data.accuracy+"% of "+
"the trials.</p><p>Your average response time was <strong>" +
subject_data.rt + "ms</strong>. Press any key to complete the "+
"experiment. Thank you!</p>";
}
};
/* create experiment timeline array */
var random_order = jsPsych.randomization.shuffle(testBlockArray);
var timeline = [];
timeline.push(welcome_block);
timeline.push(instructions_block);
for (var i = 0; i < random_order.length; i++){
timeline = timeline.concat(testBlockArray[i]);
}
timeline.push(debrief_block);
/* start the experiment */
jsPsych.init({
timeline: timeline,
on_finish: function() {
jsPsych.data.displayData();
}
});
</script>
</html>