.join doesn't work in pavlovia

Hi,
I am trying to get my experiment online. Unfortunately, I am very unfamiliar with JS and currently running into some issue regarding the improper translation from Python to JS.

URL of experiment: Katharina Weiss / SC · GitLab

Description of the problem:
I have an empty textbox component (textbox_E1) which should receive keyboard input from the participant. Via the code component I only want to allow numbers.

Python Code:

keys = k.getKeys(keyList = [0,1,2,3,4,5,6,7,8,9, 'space']); 


for ch in textbox_E1.text:
   textbox_E1.text = "".join([ch for ch in textbox_E1.text if ch in ["0","1","2","3","4","5","6","7","8","9"]]);
   textbox_E1.text = textbox_E1.text.replace(' ', '')

JS Code:

keys = k.getKeys({"keyList": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "space"]});
for (var ch, _pj_c = 0, _pj_a = textbox_E1.text, _pj_b = _pj_a.length; (_pj_c < _pj_b); _pj_c += 1) {
    ch = _pj_a[_pj_c];
    textbox_E1.text = (function () {
    var _pj_d = [], _pj_e = textbox_E1.text;
    for (var _pj_f = 0, _pj_g = _pj_e.length; (_pj_f < _pj_g); _pj_f += 1) {
        var ch = _pj_e[_pj_f];
        if (_pj.in_es6(ch, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"])) {
            _pj_d.push(ch).join("");
        }
    }
    return _pj_d;
}
.call(this));
    textbox_E1.text = textbox_E1.text.replace(" ", "");
}

I am getting the following error: TypeError: _pj_d.push(…).join is not a function

I’ve read the crib sheet which pointed me to use array.join(“delimeter”); in the JS code. Have I not implemented this correctly?

I am happy to receive any input! :slight_smile:

Thanks in advance!

Best,
Kathi

Hi,

Try to separate (in python) the list comprehension from the join function i.e. write these into two different lines. Then whatever is the js translation modify the join function in js as per crib sheet

BW
Yiannis

@Yiannis thanks for your suggestion!

I’ve rewritten the python code to the following (and adjusted the js as per crib sheet):

for i in textbox_E1:
    if i in ["0","1","2","3","4","5","6","7","8","9"]:
        textbox_E1 = "".join([i for i in textbox_E1])
        textbox_E1 = textbox_E1.replace(' ', '')

Is this what you meant?

unfortunately, I am getting the same error ( TypeError: _pj_d.push(…).join is not a function) :frowning:

Hi @Kathi I have just re-read your original post and I think I understand what you are trying to achieve.

If my understanding is correct then what you need is:

  • a textbox component (set the text field to every repeat if in a loop and leave it empty)
  • a keyboard component (in the data tab set to store all keys if Ps are required to type more than one character or to last/first key if they are required to type a single character - and set as allowed keys 0-9)
  • a code component that will contain the below in the every frame tab (change accordingly if your components are named differently)
# python bit
if key_resp.keys:
    textbox.text = "".join(key_resp.keys)
else:
    textbox.text = ''
# js bit
if (key_resp.keys) {
    textbox.text = key_resp.keys.join("");
} else {
    textbox.text = '';
}

I also attach a demo

I hope this helps

BW
Yiannis
demo_txt.psyexp (10.4 KB)