Reference error: shuffle is not defined

URL of experiment: https://run.pavlovia.org/xiaotong/vd/html

Description of the problem: hi all, I got this error - shuffle is not defined - on this line shuffle(indices). Does anyone know how to make it work in pavlovia? thanks a lot, Xiaotong.

1 Like

Hi @Xiaotong,

You can use this function to shuffle a list:

// create a function to shuffle a list
function shuffle(a) {
  var j, x, i;
  for (i = a.length - 1; i > 0; i--) {
      j = Math.floor(Math.random() * (i + 1));
      x = a[i];
      a[i] = a[j];
      a[j] = x;
  }
  return a;
}

//create a list
x = [1,2,3,4,5,6,7,8] 

// and shuffle it
shuffle(x) 
7 Likes

Thank you so much @Omidrezaa, problem solved!

2 Likes

Hi, do you mind showing how you got your code to work with shuffling indices? I still haven’t been able to figure it out.

Thanks in advance!

Cam

Hi @andersoc, I just paste the following code before shuffle(indices)

function shuffle(a) {
  var j, x, i;
  for (i = a.length - 1; i > 0; i--) {
      j = Math.floor(Math.random() * (i + 1));
      x = a[i];
      a[i] = a[j];
      a[j] = x;
  }
  return a;
}

To edit your code, click view code tab. In the htlm folder, there is a experimentname.js file. Open it and then you can edit it.

1 Like

Hi Xiaotong,

Thank you for the quick response! I’ve attempted the same thing, pasting the shuffle function into a code component to read sound files, but I am still getting an error message that my variable is undefined. However, I’m trying to put one of the list indices into a sound component, which seems to be the source of the error. Perhaps the sound component has difficulties reading a list index.

Thanks again for your response.