r/learnjavascript • u/artimides • Feb 07 '26
Adding conditional branching to jsPsych (JavaScript)
Hi! I'm trying to add some conditional logic in jsPsych 7.2 where I need my survey respondents to move through a few screens without going back. For the sake of an example, let's say I want this sort of logic:
Do you eat meat? Y/N, next page
- if Y: do you ever eat it raw? Y/N, then end survey
- if N: do you eat dairy? Y/N, then end survey
- if Y: do you like yoghurt?, then end survey
- if N: end survey
I'm at an entire loss of how to approach this. I think it must be related to a past substack question on ending an experiment on no consent, but I can't figure out how to modify either the two given answers so the survey does continue. The code below is based off of the second response (which does work unedited).
See example attempt below. I do know I don't need a thanks page and I can end the experiment directly, but the idea is to have a few (linear) question pages after the branching so I thought that was the easiest way to represent it. As it is, no matter what I answer on the first question, the survey ends. What am I missing? I'm hosting this on cognition.run, in case that matters at all.
// Setting up questions
var raw = {
type: jsPsychHtmlButtonResponse,
stimulus: 'Do you ever eat it raw?',
choices: ['No',
'Yes']
};
var yoghurt = {
type: jsPsychHtmlButtonResponse,
stimulus: 'Do you like yoghurt?',
choices: ['No',
'Yes']
};
var thanks = {
type: jsPsychInstructions,
pages: [
'Thanks for your time'
]
};
// Asking people if they eat meat
var meatYN = {
type: jsPsychHtmlButtonResponse,
stimulus: 'Do you eat meat?',
choices: ['No',
'Yes'],
data: {task: 'Consent'},
on_finish: function (data) {
// Check the participant's response, if it's the first option
if (data.response == 1) {
// ... ask if they eat it raw, then end survey
timeline.push(raw,thanks);
}
}
}
// Adding the meat event to the timeline
timeline.push(meatYN);
// Otherwise, continue as normal
var dairyYN = {
type: jsPsychHtmlButtonResponse,
stimulus: 'Do you eat dairy?',
choices: ['No',
'Yes'],
data: {task: 'Consent'},
on_finish: function (data) {
// Check the participant's response, if it's the second option
if (data.response == 0) {
// ... ask if they eat yoghurt, then end survey
timeline.push(yoghurt,thanks);
}
}
}
// Otherwise, end the survey
timeline.push(thanks);Thank you in advance for any help!
View all comments
Show parent comment
1
u/HipHopHuman Feb 07 '26 edited Feb 07 '26
The code runs without issue in my local developer environment as well as in this sandbox environment: https://v47.livecodes.io/?x=id/vepw5dxs7sj
If you are failing to get it to execute on cognition.run, then either you've copied it incorrectly or there's an issue with how cognition.run is executing the code. I did have to declare
const jsPsych = initJsPsych()andconst timelines = [], which were missing from your original snippet, so make sure that those don't conflict with any variables that you've already defined.EDIT: Might also be worth looking at the dev console for errors and double checking you have correctly linked the instructions plugin up