Translate for loops into JS (random function)

URL of experiment:
RTexp1.psyexp (32.3 KB)
https://pavlovia.org/run/NiklasJung/10_exp_rt

Description of the problem: Greetings, im currently working on this “Sternberg-like” Experiment on reaction time. It works offline perfectly. Im struggling with the manual translation of the py code to the js in order to run the Experiment online. I solved the random positions of the stimuli but I need help with translating the loops.

‘’'number_symbols = random.randint(3, 7)

opa_list = [0, 0, 0, 0, 0, 0, 0, 0]

i = 0
if random.randint(0, 1)>0.5:
opa_list[0] = 1
i = 1
else:
opa_list[0] = 0

for x in range(random.randint(3, 7) - i):
opa_list[x+1] = 1
‘’’

Error message: Uncaught (in promise) ReferenceError: random is not defined RTexp1.js:344

I can provide more infos if necessary

Thanks in advance.

With kind regards,
Niklas

Try using randint instead of random.randint.

Thanks for the quick response.

'''

if randint(0, 1)>0.5:
‘’’

The web console calls:
Uncaught SyntaxError: Unexpected identifier
on this line

It worked with: function generateRandomInteger(min, max) {
return Math.floor(Math.random()*(1) - 1) } > 1

but then this line:
‘’’
opa_list[0] = 1;
‘’’

gives the error:
Uncaught SyntaxError: Unexpected identifier
(even though i defined opa_list as ‘’‘var’’’

You need randint(0,2) to generate a random choice between 0 and 1.

Then following error occurs:
Uncaught (in promise) TypeError: randint is not a function

Any ideas what went wrong?

What version of PsychoPy are you using?

image

Its the newest Version right?

v2021.2.3

1 Like