Description of the problem: Hi, I am trying to store RT in an array to later get the median of that array as the next stimulus duration.
Here’s how I store the RT in an array:
var repro1_array = new Array (6);
var reproduction1;
var i = 0;
function iteration1RoutineEachFrame(trials) {
return function () {
//------Loop for each frame of Routine 'iteration1'-------
let continueRoutine = true; // until we're told otherwise
// get current time
t = iteration1Clock.getTime();
frameN = frameN + 1;// number of completed frames (so 0 is the first frame)
// update/draw components on each frame
if ((_key_resp_allKeys.length === 2)) {
key_resp.keys = _key_resp_allKeys.slice((- 1))[0].name;
reproduction1 = (_key_resp_allKeys.slice((- 1))[0].rt - _key_resp_allKeys.slice((- 2))[0].rt);
repro1_array[i] = reproduction1;
i = i+1;
continueRoutine = false;
}
if (polygon.status === PsychoJS.Status.STARTED && t >= frameRemains) {
polygon.tStop = t; // (not accounting for frame time here)
polygon.frameNStop = frameN; // exact frame index
}
And here is how I get the median and define it as the next stimulus duration (seed 2):
var stimulus1;
var seed2;
function iteration1RoutineEnd(trials) {
return function () {
//------Ending Routine 'iteration1'-------
for (const thisComponent of iteration1Components) {
if (typeof thisComponent.setAutoDraw === 'function') {
thisComponent.setAutoDraw(false);
}
}
sum_ite1 += reproduction1;
ite1_count += 1;
const arr1Sort = repro1_array.sort();
const len = repro1_array.length;
const mid = Math.ceil(len/2);
const median1 =
len % 2 == 0 ? (arr1Sort[mid] + arrSort[mid -1]/2 : arrSort[mid -1]);
stimulus1 = (polygon.tStop - polygon.tStart);
ite1_loop.addData("stimulus1", stimulus1);
ite1_loop.addData("reproduction1", reproduction1);
ite1_loop.addData("sum_ite1", sum_ite1);
if ((ite1_count === trialNum)) {
seed2 = (median1);
ite1_count = 0;
sum_ite1 = 0;
}
As I try to run the experiment I get this error: Uncaught SyntaxError: Unexpected token ‘:’ for the line len % 2 == 0 ? (arr1Sort[mid] + arrSort[mid -1]/2 : arrSort[mid -1]);
I found this code online so I’m not sure whether it works the same way in Pavlovia. As I understood the colon operator was used to say “if mod is not 0 then take the mid value of the array”. But it didn’t work the same way here apparently. Could you help me understand why?