Reference error: Can't find variable: duration

Hello,

I keep getting the error “Reference error: Can’t find variable: duration” when I try running my Psychopy experiment on Pavlovia, but it works fine offline. Unfortunately, I’m unfamiliar with JS and currently running into some problems regarding the improper translation from Python to JS.

Python Code:
[Begin Experiment]
a=[1,2]*5
shuffle(a)

[Begin Routine]
if a[pr2.thisRepN]==1:
duration=.2
else :
duration=1.0

JS Code:
[Begin Experiment]
a=([1,2]*5);
shuffle(a);

[Begin Routine]
if ((a[pr2.thisRepN] === 1)) {
duration = 0.2;
} else {
duration = 1.0;
}

I’m not sure what else to do. (My psychopy is v3.2.4.)
Happy to receive any input! Thanks!

Hi @Shannnn,

firstly, I guess the problem is that “shuffle” is not a js function. To create a function to shuffle your array, you can have a look at this.

For example, you could copy-paste this code

function shuffle(array) {
  var m = array.length, t, i;

  // While there remain elements to shuffle…
  while (m) {

    // Pick a remaining element…
    i = Math.floor(Math.random() * m--);

    // And swap it with the current element.
    t = array[m];
    array[m] = array[i];
    array[i] = t;
  }

  return array;
}

in front of your js code. Then, the experiment “knows” what shuffle is.

Second, I don’t know if [1,2] * 5 works as you expect in js. So you may also have to find a function to repeat an array. Maybe something like this

const makeRepeated = (arr, repeats) =>
  [].concat(...Array.from({ length: repeats }, () => arr));

taken from here.