Cumulative accuracy rate no showing in online experiment

In my experiment, I want participant to receive cumulative accuracy feedback on their yes/no responses. When running online, the message doesn’t show percentage correct, it’s always Score = NaN% , but other part of the conditional feedback message shows up normally.

if ((trials.thisN === 0)) {
    number_correct = 0;
}
if (key_resp.keys.toString() === Operation_answer.toString()) {
    number_correct = (number_correct + 1);
    msg='Correct! \nScore = ' + String(number_correct*100/trials.nTotal) +'%'
    msgColor = "green";
} else {
    msg='Oops, that was wrong! \nScore = ' + String(number_correct*100/trials.nTotal) +'%'
    msgColor = "red";
}

Hi @tokaalmighty, that is JavaScript telling you there is some kind of type mismatch calling String(number_correct * 100 / trials.nTotal). Can you confirm that both number_correct and trials.nTotal are numbers? If one of them is undefined the result of that operation would be NaN if that makes any sense. Why not check ahead of assigning to msg? You could say for example,

if (number_correct !== undefined && trials.nTotal !== undefined) {
  // ...
}

Is this what you mean? I tried this, same issue

if (key_resp.keys.toString() === Operation_answer.toString()) {
    number_correct = (number_correct+1);
    if (number_correct !== undefined && trials.nTotal !== undefined){
        msg='Correct! \nScore = ' + String(number_correct*100/trials.nTotal) +'%'
    msgColor = "green";}
} else {
    msg='Oops, that was wrong! \nScore = ' + String(number_correct*100/trials.nTotal) +'%'
    msgColor = "red";
}

Well it would have to be the same for the ‘Oops…’ part no?

Or it could be that one or both of them turn up NaN as well, not undefined?

added, but same issue

Would you be happy sharing your project with me so I can look at the rest of the code?

I changed trials.nTotal to 15 just to try out if that was the issue, otherwise the code is identical to what I posted here

Thanks!

OK for some reason trials.thisN starts off at 15 and as such number_correct is never initialised to 0. It would be undefined in the first loop and ends up NaN as a result of adding 1. I have made a few tweaks in place to demonstrate a potential fix, but it would be relatively simple to transfer that into your custom code component within the PsychoPy Builder. Below is a preview. Hope that helps, s.

how do I see the code for your fix?

I link to it in the text, here it is again https://gitlab.pavlovia.org/support/written_response/blob/master/html/task%20-%20V_dec10%20-%20Copy.js#L1688

1 Like

Thanks, adding that specific line solved my issue!