Translating to JS: Function count does not exist

URL of experiment: https://pavlovia.org/run/laurapissani/typing_demo/html/

Description of the problem: I am trying to TYPE, use shift and delete in JS (to run online), the code bellow works for PY but not JS (the autotranslator is not fully working for this line of code). Any chance someone can help me correct my code and add the shift to capitalize in JS:

Ps. I have read multiple post on how to write in JS, but I still can’t fine how to delete and capitalize.

This is the PY:


L=Answer.keys
nb_back=L.count("backspace")
for i in range(nb_back):
    pos=L.index("backspace")
    if pos>0:
        L.pop(pos-1)
        L.pop(pos-1)
nb_space=L.count("space")
for i in range(nb_space):
    pos=L.index("space")
    L[pos]=' '
    
Refined_response=''.join(L)

This is the autotranslated JS (does not work):

L = Answer.keys;
nb_back = L.count("backspace");
for (var i = 0, _pj_a = nb_back; (i < _pj_a); i += 1) {
    pos = L.index("backspace");
    if ((pos > 0)) {
        L.pop((pos - 1));
        L.pop((pos - 1));
    }
}
nb_space = L.count("space");
for (var i = 0, _pj_a = nb_space; (i < _pj_a); i += 1) {
    pos = L.index("space");
    L[pos] = " ";
}
Refined_response = "".join(L);

Gives me this error:
TypeError: Cannot read property ‘count’ of undefined

Your title implies that you think count isn’t working but your actual error message doesn’t say this. It says that L is undefined. Where does Answer come from?

OH! Answer is the keyboard component for typing:

Does Answer force the end of the routine?

I don’t know if you can access Answer.keys during the routine but even if you can you need some code to cope with the situation before the first key press. Eg if Answer.keys != undefined:

Use the auto translate

Yes.

It works no problem in PY (because the return key is only for ending the routine). But this same code does not work in JS (I used the auto translate). I am not sure what you meant by

Where should I put that? is it incomplete?

You could try something like

if Answer.keys == None:
     L = []
else:
     L = Answer.keys

However, it will auto translate None to null which you will probably need to edit to undefined after switching the code component to Both.

If that doesn’t work then you could try

if Answer.keys!= None:
    Add all of the rest of your Python code here but indented so it only runs if there are Answer keys.

Again None should be translated to undefined

Type_in_response.psyexp (10.8 KB)

Still does not work, it gives me error:
https://run.pavlovia.org/laurapissani/type_py/html/

The code above definitely won’t work since you are saying if it isn’t None delete everything. Try changing != to == in the first line.