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.
Python:
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.
Python:
cond_array = random.sample(cond_array, 1) + random.sample(cond_array, len(cond_array))
Javascript:
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?