Adding and debugging custom code

URL of experiment: https://run.pavlovia.org/TheSav/conda_psychopy3-withpolys/html/

Hi All, I am attempting to debug a latent inhibition program.

I have custom code in builder which I am attempting to manually convert to JS. I’m new to JS and have attempted the following:

However, the first issue I encounter is that my variable “score_learn” is not defined and the program stalls on line 813. However, the variable is defined in the function experimentInit() on line 163. (This is done by defining it in the begin code component of my custom code - code2.)

Are variables local to the function? How do I get the program to update the variable on each trial? Sorry, but I’m beginning from a low baseline.

Thanks, Justin.

Hi @TheSav, you do not need to declare your variables because PsychoPy will do that for you, but in a way to give them global scope. If you use var score_learn = 0; , then you will limit the scope of the variable to the function in which is was declared (e.g., experimentInit()), and you will get a ReferenceError if you use that variable later in the task. So, just remove the var and you’re good to go.

To add data using JS, the call is slightly different:

 psychoJS.experiment.addData("column", data)

Something to bear in mind using ++ and --. The value these operations return and the value they store are different (i.e., value returned is not updated). You can always use myVar += 1;.

Thanks @dvbridges that’s fixed these issues.