Conditional loop not working #2137
-
Hi, I am trying to create a condition where only some trials will be presented and used a conditional loop as you offered before, but it did not seem to work for me. {
sentence: "The bla bla. ",
question: "Was the bla bla?",
correct_response: "j",
condition: "v1",
task_part: "sentences",
} For some trials, I do not want to present a question, and instead, under question I have null. Unfortunately, the condition does not work as I wanted, and instead of presenting no question, it preset the word null as a question. The code: var sentence = {
type: 'html-keyboard-response',
stimulus: function() {
return '<span style="font-size:25px;">' + jsPsych.timelineVariable('sentence', true) + '</span>';
},
choices: ['space'],
post_trial_gap: 500,
prompt: '<br><br> Press the space bar to continue',
};
var cursor_off = {
type: 'call-function',
func: function() {
document.body.style.cursor= "none";
}
};
timeline.push(cursor_off);
var question = {
type: 'html-keyboard-response',
stimulus: function() {
return '<span style="font-size:25px;">' + jsPsych.timelineVariable('question', true) + '</span>';
},
choices: ['f','j'],
post_trial_gap: 1000,
prompt: '<br><br> NO - <strong>F</strong> YES - <strong>J</strong>',
data: {
sentence: jsPsych.timelineVariable('sentence'),
question: jsPsych.timelineVariable('question'),
condition: jsPsych.timelineVariable('condition'),
correct_response: jsPsych.timelineVariable('correct_response'),
task_part: jsPsych.timelineVariable('task_part'),
trial_part: 'question',
},
on_finish: function(data) {
var acc = false;
if (data.correct_response == jsPsych.pluginAPI.convertKeyCodeToKeyCharacter(data.key_press)) {
acc = true;
}
data.accuracy = acc;
}
};
var question_conditional = {
timeline: [question],
conditional_function: function() {
// check whether or not the question trial should be presented
if (jsPsych.timelineVariable('question') == null) {
return false;
} else {
return true;
}
}
}
var sentence_procedure = {
timeline: [sentence, question_conditional],
timeline_variables: main_sent_info,
randomize_order: true
};
timeline.push(sentence_procedure); I have tried first to print to console the question, and could not find a way that presented the question itself. Maybe it's because I'm not saving the data as I go along just at the end? var trial_sent=jsPsych.timelineVariable(main_sent_info); //presents the entire stimuli list
console.log(trial_sent); and: var trial_sent=jsPsych.timelineVariable(main_sent_info); //presents general stimulus info, at the beginning, without changing every trial
console.log(trial_sent.question); and: var trial_sent = jsPsych.data.getLastTrialData(); // same as before, general info, at the beginning of the exp, not changing
console.log(trial_sent); I'm not sure how to refer to the actual question I need for the condition to be compared properly. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @Altr37, I can't remember what version of jsPsych you're using, but if it's not the most recent version then you may need to add the second var question_conditional = {
timeline: [question],
conditional_function: function() {
// check whether or not the question trial should be presented
if (jsPsych.timelineVariable('question', true) == null) { // <--- added true argument here
return false;
} else {
return true;
}
}
} Also, just to check: in the cases where you don't want a question, is the question value just the word {
sentence: "The bla bla. ",
question: null,
//...
} Finally, when trying to debug this problem, it wasn't clear to me where you were putting the console logs in the last part of your question. What I would do is try to see what the value of var question_conditional = {
timeline: [question],
conditional_function: function() {
// check the value of 'question' on this trial
var question = jsPsych.timelineVariable('question', true);
console.log('question: ', question); // <---- added console.log here
// check whether or not the question trial should be presented
if (jsPsych.timelineVariable('question', true) == null) {
return false;
} else {
return true;
}
}
} Does this help at all? |
Beta Was this translation helpful? Give feedback.
Hi @Altr37, I can't remember what version of jsPsych you're using, but if it's not the most recent version then you may need to add the second
true
argument to `jsPsych.timelineVariable('question') inside your conditional function, like this:Also, just to check: in the cases where you don't want a question, is the question value just the word
null
(with no quotes around it…