Shuffling list array in javascript

URL of experiment: n/a

Description of the problem: I have a conditions array in a experiment that I am trying to convert to online, but I am having trouble figuring out how to work with an array that is a list of lists in javascript. I keep getting javascript errors about the list being undefined.

The list is defined at the beginning of the experiment.

cond_array = 4*[["s","s","t","i"],["s","s","t","c"],["s","s","b","i"],["s","s","b","c"],["s","l","t","i"],["s","l","t","c"],["s","l","b","i"],["s","l","b","c"],["l","s","t","i"],["l","s","t","c"],["l","s","b","i"],["l","s","b","c"],["l","l","t","i"],["l","l","t","c"],["l","l","b","i"],["l","l","b","c"]]

Javascript (automatically converted):

cond_array = (4 * [["s", "s", "t", "i"], ["s", "s", "t", "c"], ["s", "s", "b", "i"], ["s", "s", "b", "c"], ["s", "l", "t", "i"], ["s", "l", "t", "c"], ["s", "l", "b", "i"], ["s", "l", "b", "c"], ["l", "s", "t", "i"], ["l", "s", "t", "c"], ["l", "s", "b", "i"], ["l", "s", "b", "c"], ["l", "l", "t", "i"], ["l", "l", "t", "c"], ["l", "l", "b", "i"], ["l", "l", "b", "c"]]);

In a later routine, I shuffle the list before each block.

cond_array = random.sample(cond_array, 1) + random.sample(cond_array, len(cond_array))


cond_array = (random_item(cond_array) + shuffle(cond_array));

random_item is the following function defined at the beginning of the experiment to select just one item from an array:

random_item = function (arr) {
    return arr[Math.floor(Math.random()*arr.length)];   

The first error occurs with arr.length in this function, because javascript says cond_array is undefined.

Is there a better way to define the list for javascript?

Hi Joseph,

I’m good at JS, so I can help you out over there. I’m not that good at Python though. Could you tell me what kind of result you expect from the sampling procedure? Maybe give an example output?

Best, Thomas

Hi Thomas, thanks for the help. I have 4 different condition settings with 16 different options. This 16 list array gets multiplied by 4 to create 64 trials. The random_item(cond_array) + shuffle(cond_array) code is meant to generate 65 random conditions because the first trial is a learning trial.

Joseph M. Orr, PhD
Assistant Professor
Department of Psychological and Brain Sciences
Texas A&M Institute for Neuroscience
Texas A&M University
College Station, TX

Thanks! I’ll make a simplified version of your algorithm to illustrate the steps. I checked my steps by running the statements below via the browser console (press F12 in Chrome/Firefox etc.).

Generating cond_array

These are JS functions I used:

// Generate cond_array with two occurences of ['a','b'] and two ['c','d']
// NB - It's a bit less elegant than in Python
cond_array = [].concat(Array(2).fill(['a','b']), Array(2).fill(['c','d']))

Shuffling cond_array

We’ll use the Fisher-Yates algorithm. Note that PsychoJS also implements a version of this, but I adopted a version from a website so we can run this prototype in the console (no dependencies).

// 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;

I hope this helps you out.

Best, Thomas

Terrific, thanks a lot!

1 Like