Specify nReps with a variable name

URL of experiment:

Description of the problem:
hi there,

I want to specify nReps based on a condition. In the code below, I specified nReps as “a” which is 1 if a condition is satisfied, and 0 if otherwise. so, n is an integer (either 0 or 1). but the below code gives error ( TypeError: Cannot read property ‘0’ of undefined) unless I manually type 1 or 0 instead of “a” to specify nReps.

can anybody help me how to assign a value to nReps by using the “a” variable instead of having to manually type an integer?

function cond25LoopBegin(thisScheduler) {
// set up handler to look after randomisation of conditions etc
cond25 = new TrialHandler({
psychoJS: psychoJS,
nReps: a, method: TrialHandler.Method.RANDOM,
extraInfo: expInfo, originPath: undefined,
trialList: ‘dummyCond25.xlsx’,
seed: undefined, name: ‘cond25’
});
psychoJS.experiment.addLoop(cond25); // add the loop to the experiment
currentLoop = cond25; // we’re now the current loop

// Schedule all the trials in the trialList:
for (const thisCond25 of cond25) {
const snapshot = cond25.getSnapshot();
thisScheduler.add(importConditions(snapshot));
thisScheduler.add(durationPresentation25RoutineBegin(snapshot));
thisScheduler.add(durationPresentation25RoutineEachFrame(snapshot));
thisScheduler.add(durationPresentation25RoutineEnd(snapshot));
thisScheduler.add(SL_response25RoutineBegin(snapshot));
thisScheduler.add(SL_response25RoutineEachFrame(snapshot));
thisScheduler.add(SL_response25RoutineEnd(snapshot));
thisScheduler.add(conf_response25RoutineBegin(snapshot));
thisScheduler.add(conf_response25RoutineEachFrame(snapshot));
thisScheduler.add(conf_response25RoutineEnd(snapshot));
thisScheduler.add(endLoopIteration(thisScheduler, snapshot));
}

return Scheduler.Event.NEXT;
}

“a” is defined above in the code by the way. just didnt copy that part in here.

thanks in advance, tutku

Did you put a or $a in the nReps field?

Without the $ is correct.

I did just as here. not “$a” but “a”

In that case my next thought is to use a different variable name, in case a is being used for something else.

did that. the problem persists though…

ok i solved the problem.
it didnt work before because the first time i was defining the “a” variable was within a function. however, where i used “a” as a value for nReps was another function. thus, in the first function where i defined “a” for the first time, “a” became a local variable, instead of a global one. hence the function where we define nRep didnt recognize “a” as a variable and asked me to define it again. i did it but when you do that the second time, the two “a” s make two different local variables: in the first function, indeed it takes an integer value. but when you define “a” with “var a;” and use it as an nRep assignment right away, js goes like “what is ‘a’ anyways? it doesnt have a proper value”, and throws an error.

once you make the variable a global one by defining it outside the function (i suggest this approach if you are to use that variable in another function), you dont face this problem. otherwise, things can happen…

Do you mind posting your code?

I’m facing a similar issue - I’m not sure how to define a global variable in code components in a way that will allow the variable to pass to a function…

In Begin Experiment, I have:


var stim_lists = ["Image_List_1","Image_List_2","Image_List_3","Image_List_4","Image_List_5"];
var stim_selection = stim_lists[Math.floor(Math.random()*stim_lists.length)];

But if I try this in Begin Routine:


Scene = new visual.ImageStim({"win": psychoJS.window, "name": "Scene", "units": "height", "ori": 0, "pos": [0, 0], "size": [0.8, 0.8], "color": [1, 1, 1], "colorSpace": "rgb", "opacity": 1, "flipHoriz": false, "flipVert": false, "texRes": 128, "interpolate": true});
Scene.setImage("stimuli/" + stim_selection  + "/" + eval(stim_selection ));

dear smweis,

well you don’t define the variable as a global variable in psychopy builder, but you do that in your js code. what i mean by “define as global variable” is simply that you define it with “var xxx;” outside all functions - e.g., above all the function definitions. for example you could define it - meh, idk - in the first line of your js code maybe? or right after the part that initializes your experiment in your js.

the critical point is that you define your variable as a variable that is accessible by all the functions in your code. that is what makes a variable a “global” variable.

1 Like

Hi there,
Go to view code in pavlovia. Find JS script and edit it. In the edit mode, around the beginning section of the script you will find lots of global variable named in this fasion ‘var xyzs’. Just put your variable there. make sure that it not within a {} brackets. And commit the changes. Beware that when you declare variable in this manual fashion, you have to repeat this process every time you sync your builder code. Visually make sure that your var is still there.

1 Like