URL of experiment: https://pavlovia.org/nicbadcock/pab
Description of the problem: Repeating a string as a list in PsychoPy/Python is straight forward:
my_colour = "green"
number_repeats = 5
my_colour_list = [my_colour] * number_repeats
print(my_colour_list)
returns: [‘green’, ‘green’, ‘green’, ‘green’, ‘green’]
The PsychoPy to PsychoJS (I’ll use Py to JS to denote this) translation recommends:
my_colour = "green"
number_repeats = 5;
my_colour_list = ([my_colour] * number_repeats);
But this returns: NaN
Though not as elegant as a single line, it could be done with a loop. Py to JS suggests:
my_colour = "green";
number_repeats = 5;
my_colour_list = [];
for (var index = 0, _pj_a = number_repeats; (index < _pj_a); index += 1) {
my_colour_list.append(my_colour);
}
But PsychoJS doesn’t recognise the append element. Though push seems to work (found here):
my_colour = "green";
number_repeats = 5;
my_colour_list = [];
for (var index = 0, _pj_a = number_repeats; (index < _pj_a); index += 1) {
my_colour_list.push(my_colour);
}
So, I have a solution but I would prefer to learn how to do it in a single line if possible. I can see that there’s a java Class called Collections that has an option:
my_colour = "green";
number_repeats = 5;
my_colour_list = Collections.nCopies(number_repeats, my_colour);
but I don’t know how to import this into my PsychoJS script.
So I’ve ended up with two questions here:
1. Is there a single line of PsychoJS code that can repeat a string x times as a list/array?
2. How do you import external java classes into PsychoJS?
PS I’m not sure if there’s a way to flag (or tag) the need for Py to JS translation updates within a post - hopefully someone with the power spots it.