Replies: 3 comments
-
George: Here is an example of html code for the simple example you give. There may be better ways to do it, but the below will ask the user if s/he is a cat or dog person, and then ask about the user's favorite breed of cat or dog depending on the first answer. I verified functionality for this script using jsPysch v7.3.3. The key plug-ins are Survey Multi Choice and Survey Text. Regards, Nick <title>Conditional Question Experiment</title> <script src="https://unpkg.com/jspsych@7.3.3"></script> <script src="https://unpkg.com/@jspsych/plugin-survey-multi-choice@1.1.2"></script> <script src="https://unpkg.com/@jspsych/plugin-survey-text@1.1.2"></script> <script src="https://unpkg.com/@jspsych/plugin-preload@1.1.2"></script> <script>
/* var conditionalTimeline = {
|
Beta Was this translation helpful? Give feedback.
-
Dear Nick,
Many thanks for this, I will test it and report back.
Best Regards,
George
…On Mon, 31 Mar 2025 at 16:53, Nick Livingston ***@***.***> wrote:
George:
Here is an example of html code for the simple example you give. There may
be better ways to do it, but the below will ask the user if s/he is a cat
or dog person, and then ask about the user's favorite breed of cat or dog
depending on the first answer. I verified functionality for this script
using jsPysch v7.3.3. The key plug-ins are Survey Multi Choice and Survey
Text.
Regards,
Nick
<title>Conditional Question Experiment</title> <script src="
***@***.***"></script> <script src="
***@***.******@***.***"></script>
<script ***@***.******@***.***"></script>
<script ***@***.******@***.***"></script>
<script>
/* initialize jsPsych */
var jsPsych = initJsPsych({
on_finish: function() {
jsPsych.data.displayData();
}
});
var question1 = {
type: jsPsychSurveyMultiChoice,
questions: [
{prompt: "Are you a dog or a cat person", options: ["Dog", "Cat"], required: true}
],
data: {question: 'dog_or_cat'}
};
var question3 = {
type: jsPsychSurveyText,
questions: [
{prompt: "What is your favorite dog breed?"}
],
data: {question: 'favorite_dog_breed'}
};
var question4 = {
type: jsPsychSurveyText,
questions: [
{prompt: "What is your favorite cat breed?"}
],
data: {question: 'favorite-cat-breed'}
};
var dogBranch = {
timeline: [question3],
conditional_function: function(){
var lastTrialData = jsPsych.data.getLastTrialData().values()[0];
if(lastTrialData && lastTrialData.response){
return lastTrialData.response.Q0 == "Dog";
}
return false;
}
};
var catBranch = {
timeline: [question4],
conditional_function: function(){
var lastTrialData = jsPsych.data.getLastTrialData().values()[0];
if(lastTrialData && lastTrialData.response){
return lastTrialData.response.Q0 == "Cat";
}
return false;
}
};
/* var conditionalTimeline = {
timeline: [question3, question4],
conditional_function: function() {
//Get the last response
var lastTrialData = jsPsych.data.getLastTrialData().values()[0];
//Branch logic
if(lastTrialData && lastTrialData.response){
var response = lastTrialData.response.Q0;
if(response == "Dog"){
return true;
}else if(response == "Cat"){
return false;
}
}
return false;
}
}; */
var timeline = [question1, dogBranch, catBranch];
/* start the experiment */
jsPsych.run(timeline);
</script>
—
Reply to this email directly, view it on GitHub
<#3521 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABXSHNZDM4ARX3LIGW5Y4EL2XFQGDAVCNFSM6AAAAABZ3GQIH6VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTENRXHA2TINQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Dear Nick,
Apologies for the late reply, have been working on some other stuff
non-stop and just got the chance to try your suggestion. It works like a
treat, just a few questions (due to my ignorance of the avalanche of
possibilities with jsPsych):
i) I guess there would be no problem adding an id to the questions, so that
when saving the answers to a CSV, they would come under the respective
columns, would it?
ii) As I understand it, you have the conditional function that does the
trick, but I am puzzled that you are not checking what the previous
response is, you are only checking if there is trial data and getting the
response item, but you are not doing any checking against the actual value.
iii) Would it work if I have that kind of function at multiple points
during the trial? For example, you might have a decision point after
question 2 and another one after question 12 (reading from
https://www.jspsych.org/v7/overview/timeline/, it says that The conditional
function is evaluated whenever jsPsych is about to run the first trial on
the timeline.)
iv) Can that work with free text answers if users are asked to type
something specific (and assuming they do it of course) - this is why I was
puzzled that we are not checking against the actual response by the user?
Best Regards,
George
…On Mon, 31 Mar 2025 at 16:53, Nick Livingston ***@***.***> wrote:
George:
Here is an example of html code for the simple example you give. There may
be better ways to do it, but the below will ask the user if s/he is a cat
or dog person, and then ask about the user's favorite breed of cat or dog
depending on the first answer. I verified functionality for this script
using jsPysch v7.3.3. The key plug-ins are Survey Multi Choice and Survey
Text.
Regards,
Nick
<title>Conditional Question Experiment</title> <script src="
***@***.***"></script> <script src="
***@***.******@***.***"></script>
<script ***@***.******@***.***"></script>
<script ***@***.******@***.***"></script>
<script>
/* initialize jsPsych */
var jsPsych = initJsPsych({
on_finish: function() {
jsPsych.data.displayData();
}
});
var question1 = {
type: jsPsychSurveyMultiChoice,
questions: [
{prompt: "Are you a dog or a cat person", options: ["Dog", "Cat"], required: true}
],
data: {question: 'dog_or_cat'}
};
var question3 = {
type: jsPsychSurveyText,
questions: [
{prompt: "What is your favorite dog breed?"}
],
data: {question: 'favorite_dog_breed'}
};
var question4 = {
type: jsPsychSurveyText,
questions: [
{prompt: "What is your favorite cat breed?"}
],
data: {question: 'favorite-cat-breed'}
};
var dogBranch = {
timeline: [question3],
conditional_function: function(){
var lastTrialData = jsPsych.data.getLastTrialData().values()[0];
if(lastTrialData && lastTrialData.response){
return lastTrialData.response.Q0 == "Dog";
}
return false;
}
};
var catBranch = {
timeline: [question4],
conditional_function: function(){
var lastTrialData = jsPsych.data.getLastTrialData().values()[0];
if(lastTrialData && lastTrialData.response){
return lastTrialData.response.Q0 == "Cat";
}
return false;
}
};
/* var conditionalTimeline = {
timeline: [question3, question4],
conditional_function: function() {
//Get the last response
var lastTrialData = jsPsych.data.getLastTrialData().values()[0];
//Branch logic
if(lastTrialData && lastTrialData.response){
var response = lastTrialData.response.Q0;
if(response == "Dog"){
return true;
}else if(response == "Cat"){
return false;
}
}
return false;
}
}; */
var timeline = [question1, dogBranch, catBranch];
/* start the experiment */
jsPsych.run(timeline);
</script>
—
Reply to this email directly, view it on GitHub
<#3521 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABXSHNZDM4ARX3LIGW5Y4EL2XFQGDAVCNFSM6AAAAABZ3GQIH6VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTENRXHA2TINQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Dear All,
Is it possible in jspsych to send the user to a specific question given their answer to a previous one (e.g. if you have a starting question saying are you a dog or a cat person, to then say go to question three if your answer was dog and go to question four if your answer was cat)?
Best Regards,
George
Beta Was this translation helpful? Give feedback.
All reactions