Translating PsychoPy to Java Script

Hi! Can anyone please help me translating the following code from python to java script?

I have made an experiment in psychopy builder (version 3.1.5 - otherwise the code component doesn’t work), which I would like to put online via pavlovia. I have a single code component in my experiment, that needs to be translated in order of doing so.
I have no idea how to do that, since I’m fairly new at this whole coding world.

My computer runs MacOS Catalina, version 10.15.

#Begin routine

if len(key_resp_4.keys): # I have created a key response in the previous routine
if ‘j’ not in key_resp_4.keys:
continueRoutine = False # the routine stops if a certain key is given

Thanks in advance and please let me know if you need more info!

Here’s a screenshot, if that’s easier.

Hi @Sigrid_Nielsen, try:

if (key_resp.keys.length > 0) {
  if (key_resp.keys.indexOf('j') > -1) {
    continueRoutine = false;
  }
}

I think it works, thank you!

Regards, Sigrid

@Sigrid_Nielsen, there is a typo in that solution, since you want to make sure that if ‘j’ is not pressed, the routine should end. If that is the case, use:

if (key_resp.keys.length > 0) {
  if (key_resp.keys.indexOf('j') === -1) {
    continueRoutine = false;
  }
}

Both works as far as I can tell, but I have changed my code to this one. Again, thanks!

Update! The code dosen’t work.

When I ran the experiment in PsychoPy it accepted the code no matter what, because it ignores the JC code and only uses the python code - until you try to run it online using Pavlovia. That’s why both codes “worked” for me - because the program ignored them.

But now I am trying to get it to work online and I can conclude that the code doesn’t work. Do you have another suggestion?

Ok, well I am not sure what is happening. Are you changing the code type in the code component to “Both”, so you have Python on the left, and JS on the right?

Yes, I have done exactly that.

Try moving the code to the “Each Frame” tab and trying again.

I’m pretty positive that it works. Thank you so much!