Problem with experimental randomization on Pavlovia

Hi, I am trying to create an experiment with six different experimental conditions online.

I made a Coding component with JavaScript to pick a random condition:

const conditions = ["1", "2", "3", "4", "5", "6"];
const randomCondition = conditions[Math.floor(Math.random() * conditions.length)];

Then, I thought about creating six routines, one for each conditon. If „randomCondition“ = 1, the first routine should be displayed and the others skipped, if „randomCondition“ = 2, the second routine shout be displayed and the others skipped and so on.
I generated the following CS code for the first condition and inserted it on the Coding component of the first routine:

if (randomCondition = 1) {
continueRoutine=True;
} else {
continueRoutine=False;
}

But when I try running the experiment online, this error appears:

ReferenceError: Can’t find variable: randomCondition

Do you have an idea how to fix it or how to code that depending on the condition specific routines should be displayed and others skipped?

Thank you very much

Sina

You have a single = in your if statement

Also I think that your code needs to go into Each Frame but could just be if != 1 rather that saying continue every Frame

Hey wakecarter, thanks for your quick response. JavaScript is very new for me so I appreciate every idea you have.

But even if I use == or != in my if statement the error stays the same:

  • ReferenceError: Can’t find variable: randomCondition

Maybe there is a problem with my first Code?

const conditions = ["1", "2", "3", "4", "5", "6"];
const randomCondition = conditions[Math.floor(Math.random() * conditions.length)];

Thanks for your help!

Are you using auto translate?

I have a crib sheet (search this forum to find it )which says what doesn’t work so I don’t pay attention to JavaScript that does work. I find it easier to look at the Python

Are your definition lines in a Begin Experiment tab?

I created my experiment with the Builder from Psychopy and everything worked fine on my computer.
For the randomization I used this Code in part “Begin Experiment” of the Coding Component:

import random 
condition = random.choice(('1','2', '3', '4', '5', '6'))

The Auto -> CS translator gave me this CS-Code:

import * as random from 'random';
var condition;
condition = random.choice(["1", "2", "3", "4", "5", "6"]);

I have six csv-files for my experimental conditions named Order1.csv, Order2.csv, Order3.csv and so on. Then, when the different conditions play a role, I told the loop for “Conditions”: "$‘Order’ + condition + ‘.csv’ ". This works fine offline and the program displays the content of the specific csv-file.

But trying to run the experiment online resulted in the error “initialising the experiment” (as I can see in your crib sheet a common error using the import function). So I filled in manually this code in the “Begin Experiment” part of the Code component and changed the “Conditions” from the loop into "$‘Order’ + randomCondition + ‘.csv’ "

const conditions = ["1", "2", "3", "4", "5", "6"];
const randomCondition = conditions[Math.floor(Math.random() * conditions.length)];

Now the experiment does not stuck anymore in “initialising the experiment” but the error * ReferenceError: Can’t find variable: randomCondition appears. Thats why I thought I change my plan and create one routine for every condition so I can code something like “if randomCondition is 1, play this routine and skip the others” and so on for the other conditions (see my initial post).

I am a little desperate since I don’t know what to change in my experiment to make it run online.

I think that any experiment with an import in the JS code is going to fail on initialisation.

Check out my crib sheet for some tips for random.

Best wishes,

Wakefield