Translation to JS problem in StroopExtended demo (adding Feedback)

URL of experiment:

Description of the problem:
Hi all, I tried to add some text feedback to my future online experiment (as shown here, but with text object) , but choosing code type “Auto–>JS” gave me the following “/* Syntax Error: Fix Python code */”.
Then I opened Stroop Extended demo, feedback routine --> message piece of code --> begin routine and found the same Syntax error message there too.
Here is the code I wanted to use for my experiment from the demo version:

if resp.corr:#stored on last run routine
  msg="Correct! RT=%.3f" %(resp.rt)
else:
  msg="Oops! That was wrong"

What is wrong with Syntax here?
I get this error even when I start typing
if key_press.corr or simply start with if.
Thank you,
Dina.

@tsukit86, I think the error is due to the string formatting, this is not a compatible with the auto-translator. Try the following:

if resp.corr:#stored on last run routine
  msg="Correct! RT=" + str(resp.rt)
else:
  msg="Oops! That was wrong"

As you want to figure to 3 decimal places, you will have to modify the JS code to use the toFixed method, so after translation ,set the code type to “Both” in the code component and use

resp.rt.toFixed(3)
1 Like

For rounding I’ve been using something like round(resp.rt*1000)/1000 which translates correctly if you add a JavaScript function

round = function (a) {
    return Math.round(a);
}

or possibly alias

round = Math.round(a);

to code_JS in the first routine.

2 Likes

@wakecarter, good idea. Here is something that might be useful to add in order to give the user the number of rounding digits

round = function(num, n=0) {    
    return +(Math.round(num + ("e+" + n))  + ("e-" + n));
}

See link.

3 Likes

I want to thank everyone for your prompt replies.
It took me some time to process and understand them, as well as to come up with new issues :slight_smile: Obviously, you replies helped me to overcome “wrong syntax” error in Python–>Java window. Thank you very much!

Hey,

I am having the same issue with too many decimals shown while presenting feedback. And I am not completely sure where to put:

resp.rt.toFixed(3)

in the same code component right before the msg? Or at the beginning of the experiment, in a code component solely for JS? Either way, it did not seem to work. So I might be missing something.

This is the code component in which correct trials and rt are calculated and presented. The formula for mean_rt_word is rather long, so I spare you with it :).

cor_trials = (counter/36)*100
cor_trials.toFixed(1)
mean_rt_word.toFixed(2)
msg = “Du hast " + money_low + " Euro gewonnen. Deine mittlere Reaktionszeit war " + mean_rt_word + " ms. Du hattest " + cor_trials + " % korrekte Antworten. Weiter mit Leertaste”;

appreciate your help

best regards
Leif

Hi!
i am trying to add this JS code but it seems to get my experiment stuck (it wont initialize)
I need 3 decimal places
i tried both resp.rt.toFixed(3)
but it doesnt seems to change any thing

if (resp.corr) {
    msg = ("Correct! RT=" + resp.rt.toString());
  
} else {
    msg = "Oops! That was wrong";
}
resp.rt.toFixed(3)

and when i tried the
round = function(num, n=0) {
return +(Math.round(num + (“e+” + n)) + (“e-” + n));
}
(this i added in the begin experiment)
it got my experiment all messed up
where do i need to place it in the code?
thank you
@wakecarter i sew you added it to the PsychoPy Python to Javascript crib sheet. could you explain to me please how to use this?

thank you!!

From 2020.2.5 onwards you need to remove this from code_JS because it is already included. You could try resp.rt=round(resp.rt,3) or you may need to set it as a new variable rt=round(resp.rt,3)

1 Like

thank you! i tried it but it still wont work

if (resp.corr) {
    msg = ("Correct! RT=" + resp.rt.toString());
} else {
    msg = "Oops! That was wrong";
}
resp.rt=round(resp.rt,3)

i also tried setting rt as a variable with no success

In what way isn’t it working? Are you getting an error message?

You could try print(resp.rt) before and after to see what’s happening to it.

1 Like

fixed it!
in the python part i wrote :

if resp.corr:#stored on last run routine
  msg="Correct! RT=" + str(round(resp.rt,3))
else:
  msg="Oops! That was wrong"

and the JS translated to

if (resp.corr) {
    msg = ("Correct! RT=" + round(resp.rt, 3).toString());
} else {
    msg = "Oops! That was wrong";
}

Thank you!! @wakecarter

Thanks @chayabenyehuda

I’ve used the same code for feedback trials and I couldn’t find how to print to 3 decimal places for the online (Javascript) version (it worked already in Python). I adapted your code to my variable names and this worked a treat!