psychopy.org | Reference | Downloads | Github

Convert Python to JavaScript (Pavlovia) with variables "intrinsically" created by PsychoPy


#1

Dear all,

We have a 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 (offline experiment) but not in JS (online Pavlovia version), because we encounter some problems with them:

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

Python:

if Test.thisN == 0:
   number_correct = 0

JS:

number_correct = 0

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

Under the “End Routine” tab, we have

python:

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:

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 anyone could help us with this issue? Thanks a lot!


#2

Hi @katsuragiai, I have attempted to recreate your error. I set a text component and keyboard (called “TestKey”) to repeat N times using a loop called “Test”. I then copied your JS code into a code component. I am not able to see the error. Would you mind sharing a link to your study for debugging?