Shuffling list array in javascript

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). https://medium.com/@nitinpatel_20236/how-to-shuffle-correctly-shuffle-an-array-in-javascript-15ea3f84bfb

// 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;
}
shuffle(cond_array);

I hope this helps you out.

Best, Thomas