How to get conditions on the output file in PsychoJS ? (Getting n+1 trial instead of n)

URL of experiment: Emotional Stroop [PsychoPy]

Description of the problem:

Hi !
I have a problem regarding the output file from my experiment, a simple emotional Stroop, where participants have to press the key corresponding to the color of the word.

However, as excel files could not be imported because of an error I could not resolve
( In the following thread ) , I wrote all my conditions in arrays.

The problem being that when using this command

psychoJS.experiment.addData('color', color);

I get the values of the trial following the one i need (instead of n , i get n+1)
Furthermore, the first trial doesn’t even get reported

I guess that it has to do with the fact that the experiment determines the first word BEFORE the first trial, but I tried several ways to counter this, with no success whatsoever.

I’m selecting my conditions with a variable (i) that counts +1 at each “begin routine”

Here is my code for each “begin routine” of the trial.
I know it’s a little long and maybe messy but I’m not really experimented in JS, thank you in advance for any help you can provide.

neutral = ["CUP", "SECTOR", "INFLUENCE", "SUBJECT", "FLAG","WINDOW",
"SEQUENCE", "FORK", "OAT", "HALL", "HAMMER",
"SPOON",
"PERSON", 
"WHISTLE",
"POSTER",
"GLASS",
"SHOVEL",
"COIL",
"ROD",
"MEASURE"]
positive = ["LINGERIE",
"ECSTASY",
"PARTY",
"FLIRT",
"BIRTH",
"LUCK",
"MARRIAGE",
"HUMOR",
"LOVE",
"DARLING",
"PERFUME",
"FANTASY",
"RENDEZVOUS",
"BABY",
"FUN",
"DANCE",
"TRIUMPH",
"DESIRE",
"TENDERNESS",
"FUTURE"]
negative = ["FAREWELL",
"CRASH",
"ASSASSINATION",
"THEFT",
"BURGLARY",
"DANGER",
"HOSTAGE",
"ULCER",
"HUNGER",
"CORPSE",
"MURDER",
"VICTIM",
"PLAGUE",
"DISPUTE",
"BLAME",
"GRIEF",
"ACCIDENT",
"CRIME",
"FAILURE",
"VIRUS"]

neutral = neutral.sort(() => Math.random() - 0.5)
positive = positive.sort(() => Math.random() - 0.5)
negative = negative.sort(() => Math.random() - 0.5)

i = 1
word= neutral[0]

arr_col=["red", "green", "blue","yellow"]

selection=Math.floor(Math.random() * arr_col.length);

color=arr_col[selection]

if (selection > -1) {
  arr_col.splice(selection, 1);
}

duree_croix=6

if (color=="red"){
    correct_trial="h"
    }else if (color=="green"){
        correct_trial="j"
        }else if (color=="blue"){
            correct_trial="k"
            }else if (color=="yellow"){
                correct_trial="l"
                }
            

if (color=="red"){
    real_color=[1.000,-1.000,-1.000]
    }else if (color=="green"){
        real_color=[-1.000,1.000,-1.000]
        }else if (color=="blue"){
            real_color=[-1.000,-1.000,1.000]
            }else if (color=="yellow"){
                real_color=[0.890,0.937,-0.929]
                }

And here is the one for each begin routine:



psychoJS.experiment.addData('condition', condition_val[i]);

if (arr_col.length==0){
    arr_col=["red", "green", "blue","yellow"]
    }




console.log("i",i)

if(i<30){ // de zero a vingt neuf
    
    if (i<10) { // de zero a neuf
        word=neutral[i]
        } else if (i>=10 && i<20) { // de dix a dix neuf
            word=positive[i-10]
            } else if (i>=20){ // de ving a vingt neuf
                word=negative[i-20]
                }
            } else { // a partir de trente
                    if (i<40) { // de trente a trente neuf
        word=positive[i-20]
        } else if (i>=40 && i<50) { // de quarante a quarante neuf
            word=neutral[i-30]
            } else if (i>=50){ // de cinquante a cinquante neuf
                word=negative[i-40]
                }
                }

psychoJS.experiment.addData('word', word);


do {selection=Math.floor(Math.random() * arr_col.length);

}while(color==arr_col[selection])

if (color=="red"){
    correct_trial="h"
    }else if (color=="green"){
        correct_trial="j"
        }else if (color=="blue"){
            correct_trial="k"
            }else if (color=="yellow"){
                correct_trial="l"
                }
            
console.log("correct_ans",correct_trial)

color=arr_col[selection]

if (color=="red"){
    real_color=[1.000,-1.000,-1.000]
    }else if (color=="green"){
        real_color=[-1.000,1.000,-1.000]
        }else if (color=="blue"){
            real_color=[-1.000,-1.000,1.000]
            }else if (color=="yellow"){
                real_color=[0.890,0.937,-0.929]
                }

psychoJS.experiment.addData('color', color);


if (selection > -1) {
  arr_col.splice(selection, 1);
}

if(i==1||i==11||i==21||i==31||i==41||i==51){
    duree_croix=6
    }else{
        duree_croix=1.5
        }

++i

I tried to set the ++i at the “End routine” but it gave the same result.
Then I tried to set it at the very first line of “Begin routine” but it only made things worse, by reporting the trial n+2 instead of n.
Here is an example of the type of output generated : PARTICIPANT_Emotional Stroop_2020-07-30_14h51.12.392.csv (2.7 KB)

Again thanks a lot for any help you can provide.