Reply To: Variable Math and Text Concatenation

PennController for IBEX Forums Support Variable Math and Text Concatenation Reply To: Variable Math and Text Concatenation

#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, 11 months ago by Jeremy. Reason: Made the HTML entities explicit
  • This reply was modified 3 years, 11 months ago by Jeremy. Reason: added comments about nbsp