Need javascript equivalent for python's in

URL of experiment: https://run.pavlovia.org/kc71/fok/html/

Description of the problem: I need to search a string for all of 3 different characters that will not appear together. Basically, I want to determine whether or not a text entry is correct, but still allow for spelling errors.

For example, if I was expecting the word garden to be typed, I could be reasonably certain that the participant would get the g, d, and n. So, I want to check if the letters g, d, and n were typed, but not together and not in order. There are 40 trials so there are 40 different words with 40 sets of letters to search for. The letters are kept in a conditions file - each letter gets its own column (letter1, letter2, letter3).

This python code is working successfully in the desktop version:

if letter1 in recall.text and letter2 in recall.text and letter3 in recall.text:
    recallTotal=recallTotal+1
    recallCorrect = 1
else:
    recallCorrect = 0
    runFOK=1

I tried this in JS, and thought it was working, but after a closer look it is not accurate. I also cannot figure out what it is doing from the pattern of correct and incorrect responses:

if (recall.text.includes(letter1 && letter2 && letter3) == true) {
    recallTotal=recallTotal+1;
    recallCorrect = 1;
}
else {
    recallCorrect = 0;
    runFOK=1;
}

Does anyone have a fix or a better suggestion? Here are examples of what it has marked right and wrong:

Typed word/Letters/Right-1 or Wrong-0
garden/g, d, n/1
werck/t, o, n/0
lad/l, e, g/1
kylight/s, k, l/1
poppy/p, a, r/0
clone/i, r, l/1

I did try changing the column names to something nonsensical without numbers in case the column name was an issue, but the results were the same.

Any help is greatly appreciated, thanks!

This is the third post I figured out as soon as I posted it. In case it helps, I won’t delete. For JS, you apparently have to write each condition in full. So, the code should be:

if (recall.text.includes(letter1) == true && recall.text.includes(letter2) == true && recall.text.includes(letter3) == true) {
    recallTotal=recallTotal+1;
    recallCorrect = 1;
}
else {
    recallCorrect = 0;
    runFOK=1;
}
1 Like

You might want to look into established methods for doing these sorts of things rather than inventing your own criterion. e.g. the Hamming distance between two strings is easy to compute but requires the two strings to be the same length, while the Levenshtein distance is a more comprehensive comparison that allows for the strings to differ in length.

Hi Michael,

Thank you, where on earth do I find established methods? That would be great as I’m having another translation issue that I’m sure is in established methods.

I’ve tried searching discourse, pavlovia demos and online public experiments, and the internet in general. (Part of the problem may be that I am so new I don’t know the right key terms to search for - e.g. I’m going to have to look up Hamming and Levenshtein and how those things relate to what I was trying to do.)

Yes. These are general concepts, not specific to PsychoPy. But if you hunt around, you’ll certainly find implementations for Python and perhaps for JavaScript too. (e.g. even the Wikipedia page for Hamming distance gives code to implement a Python function: Hamming distance - Wikipedia).

Hi Michael,

Thank you very much.

Is there a place to find often used code? I am working my way through an online JS course, but that’s going to take awhile…

If there’s not a place, what would I google to figure out how to have a mouse click check for the correct answer from a conditions .csv on each repeat in a loop in JS. My Python code works, but I can’t figure the JS code out from
anything I’ve found online. I have found the code for checking mouse.clicked_name against a constant, but no matter what I try, I can’t get it to work for the conditions file.

Is this something I should post as another question? Would people find it useful if I figure out an answer?

Thanks very much,

Kym

Yes, make another post and provide your working Python code. Hopefully someone can just help you translate it directly to JavaScript (I’m no use with that, I’m afraid).