TypeError: Cannot use 'in' operator to search for 'r' in undefined

URL of experiment: Amogh Joshi / stroop_new · GitLab

Description of the problem: I think autoJS isn’t converting the python code well.

I’m able to run this experiment perfectly in the desktop environment. I have written a code snippet for the stroop task that checks for keypress and gives feedback (correct/incorrect). Here is the python and JS script.

if timer.getTime() < 0:
    if letterColor == 'blue':
        if 'b' not in key_resp_practice.keys:
            feedback = "Incorrect. You should press b!"
        else:
            feedback = "Correct!"
    if letterColor == 'red':
        if 'r' not in key_resp_practice.keys:
            feedback = "Incorrect. You should press r!"
        else:
            feedback = "Correct!"
    if letterColor == 'green':
        if 'g' not in key_resp_practice.keys:
            feedback = "Incorrect. You should press g!"
        else:
            feedback = "Correct!"

Javascript

var _pj;
function _pj_snippets(container) {
    function in_es6(left, right) {
        if (((right instanceof Array) || ((typeof right) === "string"))) {
            return (right.indexOf(left) > (- 1));
        } else {
            if (((right instanceof Map) || (right instanceof Set) || (right instanceof WeakMap) || (right instanceof WeakSet))) {
                return right.has(left);
            } else {
                return (left in right);
            }
        }
    }
    container["in_es6"] = in_es6;
    return container;
}
_pj = {};
_pj_snippets(_pj);
if ((timer.getTime() < 0)) {
    if ((letterColor === "blue")) {
        if ((! _pj.in_es6("b", key_resp_practice.keys))) {
            feedback = "Incorrect. You should press b!";
        } else {
            feedback = "Correct!";
        }
    }
    if ((letterColor === "red")) {
        if ((! _pj.in_es6("r", key_resp_practice.keys))) {
            feedback = "Incorrect. You should press r!";
        } else {
            feedback = "Correct!";
        }
    }
    if ((letterColor === "green")) {
        if ((! _pj.in_es6("g", key_resp_practice.keys))) {
            feedback = "Incorrect. You should press g!";
        } else {
            feedback = "Correct!";
        }
    }
}


I get a type error. Not sure how I can solve it because I don’t know anything about JS. I looked at similar questions but I can’t figure out how I can move my keyboard thing around.

How can this ever be true? (countdown timers don’t work online)

key_resp_practice.keys is undefined untl a key is pressed. You could start with

if key_resp_practice.keys:

Wait, I’ve seen the timers code in other psychopy online experiments. Basically, I have a code element that checks the input to give feedback. How would I check for this in online experiments?