Variable not defined

I think it’s a scope issue. “this_loop_number” is defined in the begin experiment function, and then referenced inside another function that is not called from inside it. Scope is kind of a complex concept if you aren’t familiar with it, but the bottom line is that right now it’s only being defined for other things within the “begin experiment” code block, and you need it to be available globally.

The solution is actually to delete the word “var” from in front of it and just put this in your begin experiment code:

this_loop_number = 0;

This should declare it as an implicit global variable (see https://www.w3schools.com/js/js_scope.asp)

I also see something else which will cause trouble which is the line after that:

import * as random from 'random';

This doesn’t work in JS. In fact, no import statement from a code component will work in JS right now. The shuffle function you will have to re-create manually, but this is easy to do with the tools available. There’s an example function that works very well here: Reference error: shuffle is not defined

2 Likes