ReferenceError for variable that I created in code component (runs locally in Builder)

URL of experiment: Experiment | zared/diss-pilot-sentence-norming (pavlovia.org)

Description of the problem:

I created the variable ‘allStim’ in the Begin Experiment window of a code component. I have comparable python code that works in Builder. I don’t know why this variable isn’t defined.

Use the auto translate wherever possible.

If you use var to define a variable it is only available for that routine or function.

It wasn’t possible to use autotranslate in this instance. The import random was causing me problems. The hobbled-together function did what it was intended to do.

The problem was where it was being compiled. I needed an array saved to a variable to specify which rows to select from my conditions file. Thus, the variable I needed didn’t really live in a routine. Moreover, the variable that I needed was not being returned from the initialization routine in which I created it.

Still, I was able to get my randomization functions running using the following code Before Experiment in my very first routine:

python:

import random
fillArr = np.random.choice(range(5,62), size = 21,replace = False)
firFive = [i for i in range(5)]
fill = fillArr.tolist()
mystArr = np.random.choice(range(62,157), size = 32,replace = False)
myst = mystArr.tolist()
stim = fill+myst
random.shuffle(stim)
allStim = firFive + stim

Javascript:

function stimOrder (stimArray){

    //Array generator
    function generateArray (start,numbers) {
      return [...Array(numbers).keys()].slice(start)
    }

    // Shuffle function
    shuffle = function(array) {
      for(let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * i);
        const temp = array[i];
        array[i] = array[j];
        array[j] = temp;
      }
      return array;
    }

    var firFive = generateArray(0,5);
    var fill = generateArray(5,62);
    var myst = generateArray(62,157);
    shuffle(fill);
    shuffle(myst);
    var setFill = fill.slice(0,21);
    var setMyst = myst.slice(0,32);
    var stim = setFill.concat(setMyst);
    shuffle(stim);
    var allStim = firFive.concat(stim);
return allStim
}
var blankArray = [];
var allStim = stimOrder(blankArray);