Var element

Var elements (since beta 0.3)

let you manipulate variable values during run-time.

Creation:

newVar("myVar", 1000)

Note that the default value (1000 in the line just above) is the value to which the Var element is set at the very beginning of the experiment, when all newX commands are evaluated. If you pass something like Date.now(), it will always correspond to the timestamp of the beginning of the experiment. Use the command .set( v=>Date.now() ) instead if you want to set the value to the timestamp at the moment when the command is executed (see the example from .settings.local below).

Note also that if a Var element with the same name was made global before the current newVar command is executed, the current newVar command will refer back to that Var element instead of creating a new one, and the default value will be ignored (the global Var element will not be set to the default value). See the example from .settings.local below.

Example:

[js try=”true”]newVar(“trialsLeft”, 3)
,
newText(“remain”, ” Number of remaining attempts: “)
.settings.after( newText(“trial”, “3”) )
,
newTextInput(“guess”, “Guess my name”)
.settings.after( getText(“remain”) )
.print()
.wait(
getTextInput(“guess”)
.test.text( /Jeremy/i )
.failure( // Wrong guess:
getVar(“trialsLeft”)
.set( v => v-1 ) // Decrease trialsLeft
,
getText(“trial”) // Update trial’s text with it
.settings.text( getVar(“trialsLeft”) )
,
getVar(“trialsLeft”) // Disable guess if 0 attempt left
.test.is(0)
.success( getTextInput(“guess”).settings.disable() )
)
)
[/js]

The code above creates a new Var element named trialsLeft and initiates it with the value 3. It then adds a text input box to the page pre-filled with Guess my name, and followed by Number of remaining attempts: 3 to its right.
When the return/enter key is pressed while editing the text input box, the code tests whether its content is Jeremy (ignoring case). If the content of the text input box is not a match, trialsLeft is decreased by 1 and the text is updated. Additionally, if 0 was reached, the text input box is disabled, thus preventing further proceeding.

Commands

[yadawiki-list category=”NonTest command,Var element”]

Tests

[yadawiki-list category=”Test command,Var element”]