Quick help with JS translation error

URL of experiment: V1.1_shorttest_JS [PsychoPy]

Description of the problem: I get an unexpected toe error in line 146.

the issue is that I have the following python code:

def varInstance (variability, scaling, shift):
    while True:
        var = np.random.normal (loc = 0, scale = variability)
        if -3 < var < 3 :
            break
    return ((var * scaling) + shift)

I translated this into PsychoJS like this:

function varInstance(variability, scaling, shift){
    do {
        var = generateGaussian (0,variability);
        }
    while  (-3< var<3);
    return ((var * scaling) + shift);
    }

I also tried
let var = generateGaussian (0,variability);

Note: generateGaussian is defined as (taken from Javascript Gaussian function)

function generateGaussian(mean,std){
  var _2PI = Math.PI * 2;
  var u1 = Math.random();
  var u2 = Math.random();
  
  var z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(_2PI * u2);
  var z1 = Math.sqrt(-2.0 * Math.log(u1)) * Math.sin(_2PI * u2);

  return z0 * std + mean;
}

Any hints appreciated!

Hi @Marc_Buehner , this is probably erroring because the var variable used in the Python code is a JS keyword, so you will want to use another variable name e.g., my_var

1 Like