psychopy.org | Reference | Downloads | Github

ReferenceError: random is not defined

URL of experiment: https://gitlab.pavlovia.org/obemar/masterthesis-automatic-sound-symbol-matching

Description of the problem:
Hi,
I’m trying to run a language experiment online but have some issues with JS. I used Builder but needed code components. It works fine offline on my acer with windows 10.

I get past the initalizing stage and am able to enter a participant code but afterwards the “random is not defined” error pops up.
I used code to assign participants to either version 1 or 2 (via a dummy loop) and within 1 or 2 I counterbalanced the starting block (Group A or B).
To create the JS code I relied on the forum-posts on this topic and used the Math.random function which seems to be the common solution for randomization. I also deleted “var” because from what I read, it seemed to cause problems sometimes.

I have no clue about my error and would be happy if someone may have a look on my JS code. I put the experiment to public, anyway here’s some of the JS code I used to randomize and counterbalance:

assign participant to V1 or V2:
in Begin Experiment (I also had it in End Routine but it didn’t work online either)

function random_character() {
    chars = "CD";
    return chars.substr(Math.floor(Math.random()* 2), 1);
    }

nReps = random.character();

if ((nReps === "C")) {
    nReps_v1 = 1;
    nReps_v2 = 0;
} else {
    nReps_v1 = 0;
    nReps_v2 = 1;
}

counterbalance Group A or B within a version
in Begin Routine

function random_character() {
    chars = "AB";
    return chars.substr(Math.floor(Math.random()*2),1);
    }

condition = random.character();

and in the loop I entered $‘Group’ + condition + ‘.xlsx’

I also have code for a few typed responses but since I took (and modified it a little) the code from the Pavlovia Demo Experiment “textinput” and there’s no random in it, I guess this is not causing the issue.

Thanks for any help,
Maria

You seem to have both random.character and random_character.

Thank you, that solved it.

But now I get the error message “assignment to undeclared variable chars”
I tried changing the JS code to var chars = “AB” and leave it at chars = “AB”.
The former loaded forever after the participant code and the latter gave me the error message.
I also tried changing the nReps = random_character(); to v12 = random_character() because I thought maybe nReps couldn’t be used as a variable name. And I renamed my Versions C and D but that too didn’t work.

Here the codes for my group assignments:

Assignment to V1 or V2:

function random_character() {
    chars = "CD";
    return chars.substr(Math.floor(Math.random()* 2), 1);
    }

v12 = random_character();

if ((v12 === "C")) {
    nReps_vC = 1;
    nReps_vD = 0;
} else {
    nReps_vC = 0;
    nReps_vD = 1;
}

V1 group A or B

function random_character() {
    chars = "AB";
    return chars.substr(Math.floor(Math.random()*2),1);
    }

condition = random_character();

V2 group A or B:

function random_character() {
    chars = "AB";
    return chars.substr(Math.floor(Math.random()*2),1);
    }

condition = random_character();

This morning I had some trouble syncing with Pavlovia, so I deleted and uploaded it anew. Here the new link:

Thank you for your help,
Maria