# Python code conversion of JS in online experiment (Pavlovia)

**URL:** <https://discourse.psychopy.org/t/python-code-conversion-of-js-in-online-experiment-pavlovia/6277>\
**Category:** Online experiments\
**Tags:** pavlovia\
**Created:** [January 11, 2019, 7:28pm UTC](https://discourse.psychopy.org/t/python-code-conversion-of-js-in-online-experiment-pavlovia/6277 "2019-01-11T19:28:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![katsuragiai](https://avatars.discourse-cdn.com/v4/letter/k/e0b2c6/32.png) [@katsuragiai](https://discourse.psychopy.org/u/katsuragiai)\
**Post date:** [January 11, 2019, 7:28pm UTC](https://discourse.psychopy.org/t/python-code-conversion-of-js-in-online-experiment-pavlovia/6277/1 "2019-01-11T19:28:33Z")

</div>

Dear all,

We are looking to publish our psychopy experiment on pavlovia, and we understand that we need to manually translate our python code to Javascript code in order for custom code components to work. Our experiment works offline in psychopy, but not on pavlovia.

After some unfruitful searches, we still don’t understand why our simple code block does not work I’m posting the code component here.

The error message is : ReferenceError: Group is not defined

Python:

```auto
import numpy as np

result = np.random.randint(2)

if result == 0:

   Group = "BlockRandomizationFile_1.xlsx"

else:

   Group = "BlockRandomizationFile_2.xlsx"

```

JS:

```auto
var Group = "BlockRandomizationFile_1.xlsx";

var test = Math.floor(Math.random() * 2);

switch (test){

    case 0:

        Group = "BlockRandomizationFile_1.xlsx";

        break;

    case 1:

        Group = "BlockRandomizationFile_2.xlsx";

```

what we are doing here is a two step thing:

1. generate a random number between 0 and 1.

2. according to the number generated, we create a variable “Group” and assign it with one of our block order files.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [January 14, 2019, 9:56am UTC](https://discourse.psychopy.org/t/python-code-conversion-of-js-in-online-experiment-pavlovia/6277/2 "2019-01-14T09:56:51Z")

</div>

@katsuragiai, there are missing closing curly braces in the example given, although probably a copy and paste error. Try removing the `var` statement from your code component variables, because this is done for you when the code is compiled. Psychopy defines your variables created in your code component outside of your functions to give them a global scope. As you are defining your variables within functions using `var`, you are limiting your variables to a local scope, so they cannot be found outside of the function they are defined in. So, that may solve your problem. So try:

```auto
Group = "BlockRandomizationFile_1.xlsx";

test = Math.floor(Math.random() * 2);

switch (test){

    case 0:

        Group = "BlockRandomizationFile_1.xlsx";

        break;

    case 1:

        Group = "BlockRandomizationFile_2.xlsx";
}

```

---

<div class="post-metadata">

**Author:** ![katsuragiai](https://avatars.discourse-cdn.com/v4/letter/k/e0b2c6/32.png) [@katsuragiai](https://discourse.psychopy.org/u/katsuragiai)\
**Post date:** [January 16, 2019, 8:10pm UTC](https://discourse.psychopy.org/t/python-code-conversion-of-js-in-online-experiment-pavlovia/6277/3 "2019-01-16T20:10:19Z")

</div>

@dvbridges, Thank you so much for your reply. And yes, you pointed out our problem. After removing the “var” before variable definition, this code block works and we can randomize between different block orders. Within this code component, all the variables are defined by ourselves.

In a different part of the experiment, we have an additional question related to a variable that seems to be “intrinsically” created by psychopy. In this section, we want to make sure participants understand the instructions of the experiment, so we ask them 5 questions about the experiment. If they get all 5 correct, they move on in the experiment; if they get \<5 correct, they have to take the test over again. This repeats until they get all 5 correct.

In order to do that, we have a loop called `Test`, where we repeated the routine of presenting subjects questions. Every time the subject answers correctly, we increase a counter by 1. If the counter reaches 5, we let them bypass a loop called `FailedLoop`. Otherwise they need to go through that `FailedLoop`, which presents them with a screen telling them they have to re-do the test. The participants are then looped back to the beginning of the test.

Here, the counter we defined is `number_correct`, and in order to make this counter change values, we used variables that are automatically and “intrinsically” created by psychopy , like `Test.thisN`, which stands for the number of occurrences of the current `Test` loop, and `Testkey.corr`, which is a Boolean variable indicating whether a response is correct or not.

It seems these variables work in psychopy but not in JS, because we encounter some problems with them:

In our `Test` loop, under the “Begin Routine” tab, we have

Python:

```auto
if Test.thisN == 0:
   number_correct = 0

```

JS:

```auto
number_correct = 0

if (Test.thisN == 0) {
    
    number_correct = 0;
    
    }

```

Under the “End Routine” tab, we have

python:

```auto
if TestKey.corr:
   number_correct = number_correct + 1

if Test.thisN + 1 == Test.nTotal:
   if number_correct == 5:
       FailedLoop_reps = 0
       TestLoop.finished = True
   else:
       FailedLoop_reps = 1

```

JS:

```auto

if (TestKey.corr == 1) {
    
    number_correct = number_correct + 1;
    
    }

if (Test.thisN + 1 == Test.nTotal) {
    
    if (number_correct == 5) {
       FailedLoop_reps = 0;
       TestLoop.finished = true;
       }
   else {
       FailedLoop_reps = 1;
      }
    }

```

The offline version works perfectly. But when we run the online version, we get the error :

**TypeError: Cannot read property ‘0’ of undefined**

We suspect this has something to do with the psychopy-generated variables, as mentioned above. I was wondering if you could take a look into this too? Thanks again.
