Correct answer not registered in online experiment

Hi,

URL of experiment: Michael Cutter / DigitSpan · GitLab

Experiment Description: I’m currently in the process of implementing a digit span task in PsychoPy, which seems to be working when I run it locally. However, I have now attempted to deploy this to Pavlovia, and am having difficulties. It seems to be a problem quite a few people in other topics are having, but unfortunately none of these threads seem to solve my problem.

In the digit span task a series of numbers appear one at a time in the centre of the screen. The length of the sequence varies depending how far into the task participants are. Once the sequence has been completed participants will be asked to recall the sequence, by entering each number using their keyboard, and then pressing the return key. This is done through two separate keyboard components, with one of these being used to enter the answers, and the other to submit the answer/end the routine. I have included screenshots of the keyboard response and trial routine below. After the answer is submitted a feedback screen appears, telling participants whether their answer was correct or wrong. They then move back round a loop, onto the next trial. There are also some code components, although I am not convinced these are important to the problem I am currently having – I am of course happy to give more information if someone thinks this may help though!


The problem: The problem is that the experiment is not properly registering whether the participant’s response is correct or not. For example, if I look at the results file it includes a column stating what participants entered for their answer (column A, in the screen shot below), a column including what the correct answer should be (column R), and a column saying whether or not this answer was correct (column B). I have also added a new column to my results file, which includes an Excel IF statement checking whether the contents of columns A and R are identical, just to check that there is not a subtle difference between the two columns that I am missing. If we look at rows 3, 5, and 6, the answer given is clearly correct, while in rows 2 and 4 it is wrong. However, column B says the answer is wrong regardless.

Results

I have looked into the JavaScript file that was generated when I uploaded the experiment to Pavlovia, and cannot see anything obviously wrong with the parts of the code focused on whether participants gave the right answer. I have included these bits of code for completeness. The first bit of code appears at Line 962 while the second part at Line 1130 of the generated script.

Finally, I should perhaps point out that I had never used PsychoPy/Javascript until about two weeks ago, so there’s a good chance I’ve made what would be considered a very silly/basic mistake!

     if (DSAns.status === PsychoJS.Status.STARTED) {
      let theseKeys = DSAns.getKeys({keyList: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'], waitRelease: false});
      _DSAns_allKeys = _DSAns_allKeys.concat(theseKeys);
      if (_DSAns_allKeys.length > 0) {
        DSAns.keys = _DSAns_allKeys.map((key) => key.name);  // storing all keys
        DSAns.rt = _DSAns_allKeys.map((key) => key.rt);
        // was this correct?
        if (DSAns.keys == corrAns) {
            DSAns.corr = 1;
        } else {
            DSAns.corr = 0;
        }
      }
    } ```

And later:
```javascript 
   // store data for thisExp (ExperimentHandler)
    psychoJS.experiment.addData('DSAns.keys', DSAns.keys);
    psychoJS.experiment.addData('DSAns.corr', DSAns.corr);
    if (typeof DSAns.keys !== 'undefined') {  // we had a response
        psychoJS.experiment.addData('DSAns.rt', DSAns.rt);
        } 

Thanks for any help that you can give me,
Michael

Hi @MCutter,

The problem has to do with the following statement in the ‘code’ component of your ‘trial’ routine:

if ((DSAns.keys === corrAns)) {
    DSAns.corr = 1;
} else {
    DSAns.corr = 0;
}

The ‘DSAns.keys’ variable is an array, while the ‘corrAns’ variable is a string, so they are not technically equal.

Sometimes, using the ‘==’ operator instead of the ‘===’ operator can help in situations like this, because the ‘==’ operator is less picky, but unfortunately not in your case.

The solution has two parts. The first is to convert the ‘DSAns.keys’ variable to a string:

if ((DSAns.keys.toString() === corrAns)) {
    DSAns.corr = 1;
} else {
    DSAns.corr = 0;
}

The second part is to list the ‘corrAns’ variable in your Excel conditions file as 3,2,3 instead of [“3”,“2”,“3”], because this is how the ‘DSAns.keys’ variable comes out after converting it to a string.

However, making this change to the Excel file means that the PsychoPy program no longer works. The corresponding PsychoPy statement in the ‘code’ component of your ‘trial’ routine is:

if DSAns.keys == corrAns:
    DSAns.corr = 1
else:
    DSAns.corr=0

This needs to be changed to:

if ','.join(DSAns.keys) == corrAns:
    DSAns.corr = 1
else:
    DSAns.corr=0

These changes are sufficient to make your program work.

The fundamental issue here is that the ‘DSAns.keys’ variable has a different format in PsychoJS and PsychoPy. In PsychoJS, the ‘DSAns.keys’ variable matches 3,2,3 and ‘DSAns.corr’ is automatically set correctly without the need for any coding when ‘corrAns’ = 3,2,3. (This means that the PsychoJS code listed above that sets the value of ‘DSAns.corr’ is not actually required once the format of the ‘corrAns’ variable in the Excel conditions file has been changed to ‘3,2,3’ and this code can be omitted. I’ve left it in for clarity and for symmetry with the PsychoPy code.) Conversely, in PsychoPy, the ‘DSAns.keys’ variable matches [“3”,“2”,“3”] and ‘DSAns.corr’ is automatically set correctly without the need for any coding when ‘corrAns’ = [“3”,“2”,“3”]. But to get the program working in both PsychoPy and PsychoJS, you either need to use different code, or to maintain different Excel files for the PsychoPy and PsychoJS versions of the program. The former is the simpler solution, as Pavlovia synchs the Excel files unless they are indirectly referenced in the program by a variable.

2 Likes