Variable Math and Text Concatenation

PennController for IBEX Forums Support Variable Math and Text Concatenation

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #5094
    glossaphile
    Participant

    How can I do math on two or more variables and store the result in another variable? Similarly, how can I concatenate text elements into a new text element?

    I’m trying to get my experiment to keep track of the proportion of correct responses and report it to the subject at the end of each trial as a percentage.

    #5095
    Jeremy
    Keymaster

    Hi,

    Math operations on Var elements are not totally straightforward if you’re trying to keep plain javascript minimal, here’s an example:

    newTrial(
        newVar("dividend"),newVar("divisor")
        ,
        newTextInput()
          .before( newText("Dividend:") )
          .print()
          .wait()
          .setVar("dividend")
          .remove()
        ,
        newTextInput()
          .before( newText("Divisor:") )
          .print()
          .wait()
          .setVar("divisor")
          .remove()
        ,
        newVar("result")
          .set( getVar("dividend") )
          .set( v=> v / getVar("divisor").value )
        ,
        newText()
          .text( getVar("dividend") )
          .after( newText(" divided by " ) )          // see msg below re. spaces
          .after( newText().text(getVar("divisor")) )
          .after( newText(" makes ") )                // see msg below re. spaces
          .after( newText().text(getVar("result")) )
          .print()
        ,
        newButton("ok").print().wait()
    )

    As you can see I had to use .value to retrieve the value of another Var element inside the v => ... expression.

    The code above should also give you a good idea of how to concatenate Text elements.

    Jeremy

    • This reply was modified 3 years, 12 months ago by Jeremy. Reason: Made the HTML entities explicit
    • This reply was modified 3 years, 12 months ago by Jeremy. Reason: added comments about nbsp
    #5098
    glossaphile
    Participant

    Thanks for your ever-prompt responses, Jeremy! I’ve almost accomplished what I need. The only problem now is that the computer seems to be trimming off blank space at the beginning and ends of the fixed strings. For example, my code to report reaction time to the user looks like this.

    newVar("sRT").set(getVar("RT"))
       .set(v => v/1000)
    ,
    newText("secs","000").text(getVar("sRT"))
    ,
    newText("Reaction Time: ")
       .after(getText("secs"))
       .after(newText(" seconds"))
       .print()
    ,

    Using 653 ms as an example, I should be getting “Reaction Time: 0.653 seconds,” but instead I’m getting, “Reaction Time:0.653seconds,” with the space before and after the number missing from the display despite being present in the code.

    #5099
    Jeremy
    Keymaster

    Ah, sorry, there were supposed to be HTML entities in my code above but I guess the forum somehow stripped them away

    I should make initial and last space characters count in the next releases of PennController. In the meantime, replace the space characters at the beginning or at the end of a Text element with  

    Jeremy

    #5563
    Ncomeau
    Participant

    Do you have to perform variable/text concatenation like this within a trial? I’m struggling a bit with constructing URL text to pass into my preloadzip function. Ideally I’d be able to insert the value back from getURLParameter (I’d imagine this is a variable and not text?) into some text like “myURL_” + getURLParameter(“withsquare”) +”.zip”, but I always seem to get that invalid URL message in my log…

    #5564
    Jeremy
    Keymaster

    You can definitely do what you describe, e.g.:

    PreloadZip("https://my.server/path/to/folder/archive-"+GetURLParameter("withsquare")+".zip")

    This will fetch file https://my.server/path/to/folder/archive-2.zip if you run your experiment with withsquare=2

    Jeremy

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