Wakefield's Daily Tips

Define functions to reduce manual editing of JavaScript code

Following on from Wakefield's Daily Tips - #6 by wakecarter, I usually have one code component set to Both in the first routine (which I usually call code_both). This component can be used to define functions in the Before Experiment tab in both Python and JavaScript so that they can be called later in an Auto code component.

For example, here is inverse tan, which doesn’t yet get automatically translated from math.atan() to Math.atan()

Python

import math
def atan(x):
     return math.atan(x)

JavaScript

function atan(x) {
return Math.atan(x);
}

In the experiment itself I can then use atan(x)