Reply To: Using if statements during a trial

PennController for IBEX Forums Support Using if statements during a trial Reply To: Using if statements during a trial

#5572
Jeremy
Keymaster

Hello Max,

TLDR: this is the (or rather, one) correct syntax

newTrial( "experiment" ,
    newText ("FullText", row.FullText),
    newButton("FTButton1","Proceed to next Trial"),
    newButton("FTButton2","Proceed to Comprehension Question"),
    newText("CompQ",row.CompQ),
    newButton("CQButton","Placeholder, just click to answer"),
    getText("FullText").print(),
    ( row.CompYN==Y ?
        getButton("FTButton2")
            .print()
            .wait()
            .remove("FTButton2","FullText"),
        getText("CompQ")
            .print(),
        getButton("CQButton")
            .print()
            .wait()
            .remove("CompQ","CQButton")
    :
        getButton("FTButton1")
            .print()
            .wait()
            .remove("FTButton1","FullText")       
    )
) //closes newTrial

You cannot inject if statements this way into a PennController trial, because the sequence of commands that you pass to newTrial really are arguments that you pass to a function, and javascript does not allow you to insert if statements in this kind of environment. What you can do, however, is use the so-called inline ternary conditional operator, which uses this syntax: ( test ? success : failure )

Note that the condition will be evaluated at the very beginning of your experiment, which is fine in your case because you are testing the value of a column from your table. If you wanted to test a value that is manipulated upon runtime however, say some input from your participant, you would need to use a PennController test command, to ensure that the test is performed only after input.

Jeremy