psychopy.org | Reference | Downloads | Github

Feedback after Block Rewritten in JavaScript for Online Studies

Does anybody know what would be the equivalent of the code below in JavaScript? I am trying to implement a “feedback after block”, as explained in https://www.psychopy.org/recipes/builderFeedback.html but then in JavaScript for an online study.

nCorr = trials.data['key_resp.corr'].sum() #.std(), .mean() also available
meanRt = trials.data['key_resp.rt'].mean()
msg = "You got %i trials correct (rt=%.2f)" %(nCorr,meanRt)

Hi @tjl, the method is slightly different, and requires your to sum the contents yourself because there are no built-in methods for summing as there are with Python.

// Get all correct
nCorr = 0
for (eachResp=0; eachResp<psychoJS.experiment._trialsData.length; eachResp++) 
{
  nCorr += psychoJS.experiment._trialsData[eachResp]['key_resp.corr'];
}

msg = "You got " + nCorr + " trials correct";
/**
To get the mean, sum the actual RTs as you do with the accuracy, then divide the RTs by the length of the `_trialsData` list use the length method e.g., `psychoJS.experiment._trialsData.length`.
*/

For a one-liner sum and mean method:

responses = psychoJS.experiment._trialsData  // get list of responses
nCorr = responses.reduce((a, b) => a + b['key_resp.corr'], 0)  // get sum
meanRT = responses.reduce((a, b) => a + b['key_resp.rt'], 0) / responses.length  // get mean
1 Like

Dear @dvbridges ; thank you so much for the feedback. Does

refer to the reference .xlsx file and the trials in

to the name of the Loop?

Sorry, editing this response. The psychoJs.experiment is the experiment handler object. You can access the data from within using the _trialsData list of JS objects, which will include information about your current loop.

1 Like

Hi David, thank you again for this feedback. I tried the “one-liner sum and mean method” that you described and I do get the message in Pavlovia, however the nCorr shows up as “NaN” in the experiment. Is there something I did wrong in defining the nCorr variable? (The code below is what I have in the “Begin Route” part of the Code Component)

@tjl, would you mind sharing the URL to your task?

1 Like

Hi David, is this the URL you refer to (I set the settings to public so hopefully you can access it)?
https://pavlovia.org/timlameris/tjl_bpon_test_public_18-06

Hi @dvbridges I was just wondering if the link worked and if you could find anything incoherent in my definition of nCorr in the JavaScript?

Hi @tjl, the problem is that trialList also contains entries from your other routines, such as your practice routine. So, as you iterate through the list of JS objects, and key_resp_main.corr is not found (because that particular entry was for the practice routine, and only key_resp_practice.corr exists) you end up with a NaN value. To fix, you need to check that your trial entry is correct before adding to your nCorr variable. For this, you are better off using the long version of the code:

// Get all correct
nCorr = 0
for (eachResp=0; eachResp<psychoJS.experiment._trialsData.length; eachResp++) 
{
  if ('key_resp_main.corr' in psychoJS.experiment._trialsData[eachResp]) {
    nCorr += psychoJS.experiment._trialsData[eachResp]['key_resp_main.corr'];
  }
}
1 Like