Concatenating variables and text

PennController for IBEX Forums Support Concatenating variables and text

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #8397
    mawilson
    Participant

    Following this, I’ve set up my experimment to track participants’ first-guess accuracy in a forced-choice task where they are required to select the correct option before proceeding. This seems to be working perfectly!

    However, I would also like to show participants their first-guess accuracy between training blocks if they haven’t done well enough. While I am able to get the text to display, I’m running into a weird bug, where the text after the variable’s value gets pushed to a new line. Here’s what it looks like:
    text where the characters after the variable text are on a new line
    I’d like it to all be on the same line.

    Here’s the code; following the first link, responses stores the first-guess accuracy for each trial, and grandaverage is set to the average of that accuracy for each trial. required_to_pass is a variable currently set to 0.5.

    
    newTrial('post-training',
        newVar('grandaverage')
            .global()
            .test.is(v => v >= required_to_pass)
            .failure(
                getVar('grandaverage')
                    .set(getVar('responses'))
                    .set(v => v.filter(r => r == true).length/v.length)
            )
        ,
        getVar('grandaverage')
            .test.is(v => v >= required_to_pass)
            .success(end())
        ,
        newVar('grandaveragepercent')
            .set(getVar('grandaverage'))
            .set(v => v * 100)
        ,
        newVar('responses').global().set([]),
        newText("Your first-guess accuracy was ")
            .after(
                newText()
                    .text(getVar('grandaveragepercent'))
            )
            .after(newText('%.'))
            .center()
            .print()
        ,
        newButton('Next')
            .center()
            .print()
            .wait()
    )
    

    I’ve also tried adding the .after(newText('%.')) to the newText().text(getVar('grandaveragepercent')) instead, but I get the same result.

    • This topic was modified 2 years, 2 months ago by mawilson. Reason: clarifications
    #8400
    mawilson
    Participant

    I’ve realized how to deal with my case; it’s actually pretty easy since JavaScript lets you use addition to concatenate variables of different types.

    
    newVar('grandaveragepercent')
        .set(getVar('grandaverage'))
        .set(v => Math.round(v * 100) + '%.')
    

    Then just display the text as before, minus the .after(newText('%.')).

    (I’d still be curious how this would work if I had two text variables I wanted to concatenate, since IIRC the way variables work in PCIbex wouldn’t just let you do getVar('mytextvariable1') + getVar('mytextvariable2').)

    #8419
    Jeremy
    Keymaster

    Hi,

    I’m glad you found a solution by yourself. You can use getVar("myvar")._element.value in a pure-javascript environment to retrieve the current value of the Var element, eg. getVar("mytextvariable1").set( v => v+getVar("mytextvariable2")._element.value )

    Jeremy

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