Displaying a participant's reaction time and accuracy at the end of the task

URL of experiment:

Description of the problem:

I was able to follow the instruction of this post to display a participant’s reaction time at the end of the task. In addition, I would like to display their accuracy as well, but I’m not sure how to retrieve the information for the accuracy calculation.

Here is the Python code for displaying accuracy and reaction time:

RTmean = trials.data['TrialResp.rt'].mean()
acc = trials.data['TrialResp.corr'].sum()
## calculate percent of accuracy
totalNumberTrial = trials.nTotal
pacc = (acc/totalNumberTrial)*100
msg = "You got %0.2f %% correct. \nYour response time is %.2f seconds." %(pacc, RTmean)

Here is the PsychoJS version, but it only displays the reaction time:

// Get JS array of trial objects (i.e., a list of python dicts with response data)
dat = psychoJS.experiment._trialsData
// Filter data to get correct trials
corr = dat.filter((trial) => trial['TrialResp.corr'] === 1)
// Get RTs only as an array
rts = corr.map((trial) => trial['TrialResp.rt'])
// Reduce RTs to a single number : the mean
RTmean = rts.reduce((a, b) => (a + b), 0) / rts.length
const msg = `Your response time is ${RTmean.toFixed(2)} seconds.`;

My teammate has tried this code, but it gave the error “totalTrials not found.”

totalTrials = dat.length; 
correctTrials = corr.length; 
ACCmean = (correctTrials / totalTrials) * 100;

Could anyone please suggest how to include the accuracy calculation? Thank you so much for your help.


The Python-code uses Python string-formating which is not properly auto-translated to PsychoJS. You could switch to Python string concatanation.

msg = "Your accuracy is " + str(pacc) + ". Your RT is " + str(RTmean)

auto-translates to

msg = ((("Your accuracy is " + pacc.toString()) + ". Your RT is ") + RTmean.toString());

Best wishes Jens

1 Like


Thank you for the suggestion regarding modifying the msg part. We also resolved the issue of displaying the accuracy by leveraging the answer from this post with ChatGPT. Here is the code if anyone is interested.

var nCorr = 0; // total number of correct trials
var countEachResp = 0; // total number of trials

for (let eachResp = 0; eachResp < psychoJS.experiment._trialsData.length; eachResp++) {
      if ('TrialResp.corr' in psychoJS.experiment._trialsData[eachResp]) {
        nCorr += psychoJS.experiment._trialsData[eachResp]['TrialResp.corr'];
        countEachResp++; // Increment only if 'TrialResp.corr' exists
// calculate accuracy
ACCmean = ( nCorr / countEachResp)*100;