# Reference error: shuffle is not defined

**URL:** <https://discourse.psychopy.org/t/reference-error-shuffle-is-not-defined/11556>\
**Category:** Online experiments\
**Created:** [March 17, 2020, 10:58pm UTC](https://discourse.psychopy.org/t/reference-error-shuffle-is-not-defined/11556 "2020-03-17T22:58:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xiaotong](https://avatars.discourse-cdn.com/v4/letter/x/59ef9b/32.png) [@Xiaotong](https://discourse.psychopy.org/u/Xiaotong)\
**Post date:** [March 17, 2020, 10:58pm UTC](https://discourse.psychopy.org/t/reference-error-shuffle-is-not-defined/11556/1 "2020-03-17T22:58:47Z")

</div>

URL of experiment: [https://run.pavlovia.org/xiaotong/vd/html](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.

---

<div class="post-metadata">

**Author:** ![Omidrezaa](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/omidrezaa/32/6105_2.png) [@Omidrezaa](https://discourse.psychopy.org/u/Omidrezaa)\
**Post date:** [March 18, 2020, 12:49am UTC](https://discourse.psychopy.org/t/reference-error-shuffle-is-not-defined/11556/2 "2020-03-18T00:49:08Z")

</div>

Hi @Xiaotong,

You can use this function to shuffle a list:

```auto
// 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) 

```

---

<div class="post-metadata">

**Author:** ![Xiaotong](https://avatars.discourse-cdn.com/v4/letter/x/59ef9b/32.png) [@Xiaotong](https://discourse.psychopy.org/u/Xiaotong)\
**Post date:** [March 18, 2020, 11:14am UTC](https://discourse.psychopy.org/t/reference-error-shuffle-is-not-defined/11556/3 "2020-03-18T11:14:39Z")

</div>

Thank you so much @Omidrezaa, problem solved!

---

<div class="post-metadata">

**Author:** ![andersoc](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/andersoc/32/7332_2.png) [@andersoc](https://discourse.psychopy.org/u/andersoc)\
**Post date:** [May 7, 2020, 6:35pm UTC](https://discourse.psychopy.org/t/reference-error-shuffle-is-not-defined/11556/4 "2020-05-07T18:35:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Xiaotong](https://avatars.discourse-cdn.com/v4/letter/x/59ef9b/32.png) [@Xiaotong](https://discourse.psychopy.org/u/Xiaotong)\
**Post date:** [May 7, 2020, 7:11pm UTC](https://discourse.psychopy.org/t/reference-error-shuffle-is-not-defined/11556/5 "2020-05-07T19:11:26Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![andersoc](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/andersoc/32/7332_2.png) [@andersoc](https://discourse.psychopy.org/u/andersoc)\
**Post date:** [May 7, 2020, 8:12pm UTC](https://discourse.psychopy.org/t/reference-error-shuffle-is-not-defined/11556/6 "2020-05-07T20:12:41Z")

</div>

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.
