psychopy.org | Reference | Downloads | Github

Enumerate not fully translating to JS?

Hello,

I am working on a short code component to pull out a list of words from a larger list

thisList = 18

# get word key for this block
wordIdx = [];
for i, j in enumerate(distList['list']):
    if j == thisList:
        wordIdx.append(i)

I found that in the Auto-> JS mode, the enumerate statement produces
/* Syntax Error: Fix Python code */

Curiously, if I delete i, the Auto->JS works. Any idea why this is?

@aisa2, I am not sure enumerate will work. The equivalent JS would be :

for ([i, j] of dictList['list'].entries()) {
  if (j === thisList) {
    wordIdx.append(i)  // see Wakefields crib sheet for append
  }
}

Thanks for the reply. I kinda of get this solution conceptually. Basically there’s an issue with the enumerate function translating to JS. Does this mean I would have to rewrite the entire code block in JS to make it work?

@calebo55 there are a number of Python code bits that won’t translate super well to JS. You can use the “both” option instead of Auto->JS and edit the line of JS code independently. Just make sure it saves correctly and that it doesn’t get overwritten when you open that window later.

If you’ve ever used Google translate you’ll know that when it doesn’t work it’s a good idea to change your source language (for example to remove complex words or idioms) to give the translation tool an easier task. That’s my approach to the PsychoPy auto translate.

1 Like

Ok. So I set it to Code Type = “Both” and edited my code to java script as I understand it (which is next to nothing). This is the code I changed the python to, but it still displays a syntax error. I guess I’m kind of confused about how to edit python and JS in the same window.

function clickCount(clicked, box){
    #Return the list indices of a box
    for [i, x] of clicked.entries() {
    if (x == box){
    return i+1
    }
  }
}

@calebo55 did you come here from another thread? It would be best to write your reply on your original post.

Yep I did. My apologies. I moved my responses over to the other thread.