Accessing list variable in Javascript translation

I am trying to replace a variable “face” with a new image on each trial. The image filenames are in a list called “faces”.

Everything runs fine in the Builder. Things crash online with the error “ReferenceError: faces is not defined”. I believe it’s something to do with defining a variable in the “Begin Experiment” tab as opposed to the “Begin Routine.” I’m unsure what I should do.

Thanks in advance.

I’m not very familiar with JavaScript, but I think you have to define a variable with var rather than just an = as in Python, so this line:

faces = ['Angry.png', 'Happy.png', 'Neutral.png', 'Jumbled.png'];

Appears to be translated wrong, it should be:

var faces = ['Angry.png', 'Happy.png', 'Neutral.png', 'Jumbled.png'];

Followed by some transformation to duplicate it equivalent to *50 in Python (again, JS isn’t my best language so I’d need to look that one up). Was this generated by auto-translate or did you write it yourself manually? If it’s auto-translate we’ll need to change that in the program.

I’ve tried putting “var” where you recommend in a previous version, but I still got the same error “faces is not defined.” Any other suggestions?

The code following it with “loopi” was my attempt to multiply the list by 50.


you might try this

let faces = new Array(200);
for (i = 0; i < arr.length; i++){
    faces[i] = 'Angry.png';
    faces[i] = 'Happy.png';
    faces[i] = 'Neutral.png';
    faces[i] = 'Jumbled.png';

Notice, that this will not work if your array size is not a multiple of your file names. For instance, 199 or 201 will not work. Anyway this gives you 50-times angry aso. An alternative approach in which you don’t specify the array size is the following.

let faces1 = ['angry', 'happy', 'neutral', 'jumbled'];          
let faces = ['angry', 'happy', 'neutral', 'jumbled'];
var i = 0
while(i < 49) {
    faces = faces.concat(faces1);

Notice that I initialize the array faces with some values. You have to keep this in mind for your counter. You don’t need this but then you have an array with no values.

Best wishes Jens