PennController for IBEX › Forums › Support › Variable Math and Text Concatenation › Reply To: Variable Math and Text Concatenation
April 20, 2020 at 10:51 pm
#5095
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