0

I have a PsychoPy task where participants need to make a choice between two boxes. One box "opens" by pushing the right arrow key three times. The other "opens" by pushing the left arrow key two times. By "opens" I mean that the task advances to the next routine. I have successfully coded this using Python, but to run it online, I need to use JavaScript. Nothing I've tried has worked. Here is the Python code:

if key_resp_14.keys == ['left', 'left']:
    continueRoutine = False
elif key_resp_14.keys == ['right', 'right', 'right']:
    continueRoutine = False
elif key_resp_14.keys == ['left', 'right']:
    key_resp_14.keys = []
    theseKeys = key_resp_14.getKeys(keyList=['left', 'right'])
elif key_resp_14.keys == ['right', 'right', 'left']:
    key_resp_14.keys = []
    theseKeys = key_resp_14.getKeys(keyList=['left', 'right'])
elif key_resp_14.keys == ['right', 'left']:
    key_resp_14.keys = []
    theseKeys = key_resp_14.getKeys(keyList=['left', 'right'])

I've tried using essentially the same code in JS:

if (key_resp_14.keys === ["right","right","right"]) {
    continueRoutine = false;
}

if (key_resp_14.keys === ["left","left"]) {
    continueRoutine = false;
}

etc.

I've tried various versions of the above code with no success. I've also tried creating a variable that is a vector and pushing the info from key_resp_14.keys into that vector. When I do that, I get every keystroke from the start of the task (key_resp_1.keys all the way through key_resp_14.keys), instead of just the keystrokes from key_resp_14.keys. Any help or suggestions are greatly appreciated.

  • I'm not familiar with this library, but perhaps what you're looking for is a way to compare arrays in Javascript. Check: https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript for more information. – Ivan Mar 04 '20 at 19:06

1 Answers1

0

First thing to note is that the newest version of PsychoPY (2020.1), in builder has a feature to automatically translate from Python code to JS code, so if you are using builder trying installing that update and it might work for you.

More directly to your point I do not think you need the ===, just == should work in JS. Additionally (I'm no expert on PsychJS), but I'm wondering if you are ending the routines in a way that doesn't work with PsychoPy. See this thread on there discourse discussing how to end loops in the updated version of PsychoPy. https://discourse.psychopy.org/t/loop-finished-true-no-longer-working/11190

  • Just for clarity's sake: The `===` and `==` comparators are different in Javascript. These are not used to compare arrays (lists). Find more information how these comparators work here https://stackoverflow.com/questions/523643/difference-between-and-in-javascript#523650 I'd instead suggest a Javascript method to compare arrays – Ivan Mar 04 '20 at 19:05