psychopy.org | Reference | Downloads | Github

JS do/while loops in the code component

Hi there,

I created a custom code component in the psychopy builder to shuffle the colours of the objects displayed in my experiment. After the objects are coloured, I need to calculate the average size of the objects of each colour. The issue I’m having is that I need to create a loop that shuffles the colours and calculates the averages again if the two averages are the same. I’ve tried using a do/while loop in the JS part of the code component, but I can’t get it to work on Pavlovia. Below is the code I have tried.

c = [c1, c2, c3, c4, c5, c6, c7, c8];
blueCount = 0.0;
yellowCount = 0.0;
yellowavg = 0.0;
blueavg = 0.0;
shuffle(locs);
c1loc = locs[0];
c2loc = locs[1];
c3loc = locs[2];
c4loc = locs[3];
c5loc = locs[4];
c6loc = locs[5];
c7loc = locs[6];
c8loc = locs[7];

do {
    shuffle(colors);
    c1_color = colors[0];
    c2_color = colors[1];
    c3_color = colors[2];
    c4_color = colors[3];
    c5_color = colors[4];
    c6_color = colors[5];
    c7_color = colors[6];
    c8_color = colors[7];
    
    for (var i = 0, _pj_a = colors.length; (i < _pj_a); i += 1) {
        if ((colors[i] === "blue")) {
            blueCount = (blueCount + c[i]);
        }
        if ((colors[i] === "yellow")) {
            yellowCount = (yellowCount + c[i]);
        }
    }
    yellowavg = (yellowCount / 4.0);
    blueavg = (blueCount / 4.0);
}
while (JONS.strinigify(yellowavg) == JONS.stringify(blueavg));

Thanks in advance for any help!

To find out what’s going wrong, you could take a peek at the console and/or add some debug code. See these tutorials: Thomas Pronk / assignment_stroop · GitLab

Hi Thomas,

Thank you for the suggestion!

I took a look at the console because, I keep getting an error that “c1_color” is not defined. I have a list called “colors” initialized at the start of the experiment, and each item is a string of a color name. The console is showing that c1_color = colors[0], so it seems like calling an index in the loop doesn’t seem to work. I’m not entirely sure how to fix this though.

Screen Shot 2021-05-11 at 3.37.29 PM

How are you defining colours?

Have you seen the colours section of my crib sheet?

I have seen your crib sheet - it’s so useful!!

I currently I have a list of color names, and each color name is as a string item in the list. Then the color of each circle is defined as an index in the list. For example, I have it coded like this:

colors = ["yellow", "blue", "yellow", "blue"]
c1_color = colors[0]

Using the color name has been working for me in JS! And when I try out the RBG values, the experiment gets stuck at the “initializing this experiment” screen.

Stuck at “initialising” hints at syntax error. See Thomas Pronk / tutorial_js_syntax_error · GitLab

I went through the tutorial, and it looks like a syntax error! It looks like because it’s stuck at initializing, it doesn’t show the source code. So, I went through my code components, but I just can’t find the extra “;”.

Try opening the console and then reload your experiment. Then clicking the .js file you drew a red box around should take you to the right line.

Thanks, Thomas! That worked and now I have the syntax error solved. I’m still running into the the initial error, where c1_color is not defined. I updated the list of colors so that it uses RBG codes, but that still doesn’t seem to work.

Could you try replacing that line by
var c1_color = colors[0]

That worked! Thank you so much, Thomas!!

1 Like

Ah – I think the issue is that c1_color = colors[0] was inside a do clause and therefore variables set within it haven’t been initialised. Add c1_color = ‘’ etc. in Begin Experiment should also work.

1 Like