Conditionals/display logic

PennController for IBEX Forums Support Conditionals/display logic

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #6165
    August
    Participant

    Hi Jeremy:

    I wish to display different tasks based on participants’ answers to their answers to the previous question. For instance, for people who answered ‘No’ to the question, they can continue to the next trial, but for people whose answer is ‘No’, I want to have them to do a follow-up task before going to the next trial. Is that possible?

    Thanks so much!

    Best wishes,
    August

    #6167
    Jeremy
    Keymaster

    Hi August,

    Yes, it is possible. You’ll have to use a global Var element to store and retrieve the participant’s answer to the previous trial. Then depending on its value you either run the full next trial, or you skip over it. Here’s a basic example:

    newTrial(
        newText("How many inches in a foot?").print()
        ,
        newScale("answer", '10','11','12','13','14').button().print().wait()
        ,
        newVar("inches").global().set(getScale("answer"))
    )
    
    newTrial(
        getVar("inches").test.is(12).failure(
            newText("How many centimeters in a meter?").print()
            ,
            newScale("answer", '10','100','1000').button().print().wait()
                .test.selected(100).success( newText("Good job!").print() )
                .failure( newText("Oh well, better luck next time").print() )
            ,
            newButton("Next").print().wait()
        )
    )
    
    newTrial(
        newText("This test is over!").print()
        ,
        newButton().wait()
    )

    Let me know if you have questions

    Best,
    Jeremy

    #6178
    August
    Participant

    Thanks so much!! This is super helpful!

    #9614
    laiamt
    Participant

    Hi Jeremy,
    I have a similar question to the one above. In my case, I have a checkbox scale. If participants select (at least), the third option, they should proceed to the rest of the experiment. If they don’t, they should only see the last screen.

    In my attempt, however, the experiment ends after the participants select any of the options of the scale.
    I include the code and the demonstration link below.

    Any help will be greatly appreciated.

    Best,
    Laia

    
    Sequence( "pretest", "exp", SendResults(), "end")
    
    newTrial("pretest",
        newScale("input_tag1", "eh", "no", "oi", "fa") .checkbox().log().vertical().print(),
        newVar("tag1").global().set(getScale("input_tag1")),
        newButton("go", "Endavant") .print() .wait()
    )
    
    newTrial("exp",
        getVar("tag1").test.is(oi).success(
            newText("instrus1", "You selected 'oi'!.") .print(),
            newButton("go", "Endavant") .print() .wait()
        )
    )
    
    newTrial("end",
             newText("end1", "We are done") .print(),
        newButton().wait()
    )
    

    Demonstration link: https://farm.pcibex.net/r/IvVKNg/

    #9620
    Jeremy
    Keymaster

    Hi Laia,

    Your first trial (“pretest”) shows a Scale element, immediately sets the Var element “tag1” to the currently selected value on the scale (ie. no value, since the participant hasn’t had time to make a choice yet) then prints the “go” button and wait for a click before moving on to the next trial

    You second trial (“exp”) actually contains a fatal error, so it won’t be run, but if it were, it would check that the value of that Var element corresponds to the value of the variable oi, which is not only undefined (as far as I can tell) but most importantly not declared: the script does not know what oi refers to and crashes at that point, never creating the second trial nor executing any of the code that comes after that (ie. it doesn’t create the “end” trial either)

    So you’ll want to switch the order of the last two lines of your first trial and add double quotes around oi in the second trial:

    newTrial("pretest",
        newScale("input_tag1", "eh", "no", "oi", "fa") .checkbox().log().vertical().print(),
        newButton("go", "Endavant") .print() .wait(),
        newVar("tag1").global().set(getScale("input_tag1"))
    )
    
    newTrial("exp",
        getVar("tag1").test.is("oi").success(
            newText("instrus1", "You selected 'oi'!.") .print(),
            newButton("go", "Endavant") .print() .wait()
        )
    )

    If by “selecting at least oi” you mean selecting “oi” or “fa”, you can add a disjunct to your test:

    getVar("tag1").test.is("oi").or( getVar("tag1").test.is("fa") ).success(

    Jeremy

    #9621
    laiamt
    Participant

    Hi Jeremy,
    Thanks, that’s very helpful.

    By ‘selecting at least oi’, I mean that if they select ‘oi’, they should proceed with the experiment, regardless of whether they also chose other options. So they should proceed, if they select only ‘oi’, if they select ‘oi’ and ‘fa’, if they select ‘oi, ‘fa’, ‘no’, etc…. Is there any way to do this?

    Thanks again,

    Laia

    #9623
    Jeremy
    Keymaster

    Hi Laia,

    This line newVar("tag1").global().set(getScale("input_tag1")) will set the Var element to the last selected value, so you won’t exactly get what you want indeed: if the participant checks “oi” and later on also checks “fa”, then the Var element’s value will be “fa” and you won’t detect that “oi” is still checked

    Because the Scale element only detects the most recent selection, you’ll need to use a workaround:

    newTrial("pretest",
        newScale("input_tag1", "eh", "no", "oi", "fa") .checkbox().log().vertical().print(),
        newButton("go", "Endavant").print().wait(),
        newVar("tag1").global().set( ()=>document.querySelector("input[value='oi']").checked )
    )
    
    newTrial("exp",
        getVar("tag1").test.is(true).success(
            newText("instrus1", "You selected 'oi'!.") .print(),
            newButton("go", "Endavant") .print() .wait()
        )
    )

    Jeremy

    #9637
    laiamt
    Participant

    Thanks a lot!

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.