psychopy.org | Reference | Downloads | Github

Variable not set correctly in JS

Description of the problem:

My experiment contains a set of practice trials where I provide feedback to participants
(feedback routine in practTrials)

When the participant answers correctly, which can be pressing the spacebar or no response in some cases, I wanted to display a green tick, and a red x when the answer was incorrect.

In python this is handled by the following code:

#Begin Experiment
msg=''
feedbackCol=[1,1,1]
tally=0
tot=0

#Begin Routine
tot = tot + 1
if resp.corr:#stored on last run routine
  feedbackCol=[0,1,0]  #colour of feedback text 
  msg="√"
  tally = tally + 1
  #RT=%.3f" %(resp.rt[0])
else:
  msg="x"
  feedbackCol=[1,0,0]

I tried to replicate this in JS using

#Begin Experiment
var tot = 0;
var tally = 0;

#Begin Routine
tot = tot + 1;
if (resp.corr){
    feedbackCol=[0,1,0];
    msg = "√";
    tally = tally + 1;
} else {
    msg="x";
    feedbackCol=[1,0,0];
}

This lead to rather odd behaviour where it gives correct feedback when I press space in a target trial but gives incorrect feedback for trials that require no response.

Does JS handle ‘none’ responses differently or do the msg and feedbackCol variables not get updated correctly?
I’m very new to JS so any help is appreciated!

I think in JS you need

if (resp.corr==1) {
...

because I think it doesn’t automatically evaluate 1 as meaning True

@lrier, you also need to make sure that you do not declare your variables using var. This will cause your variables have local scope in each part of the task (e.g., begin experiment function). Instead, remove the var and PsychoPy will declare yourt variables them outside of the functions, making them global to your whole task.

Unfortunately, that doesn’t change anything.
I’m not sure if the JS side of things handles None responses differently and doesn’t change resp.corr to the expected value?

Thank you for your reply! I did make that change as well but to no avail

You’d have to share the link to your actual study for us to investigate further

Hi @jon,this is my experiment
https://pavlovia.org/lr/megabit_1

I have managed to get correct responses (subject presses space in the correct interval), however, when the subject correctly omits a response the feedback text doesn’t update

I can’t access your experiment, but my suspicion is that it’s an issue with variable declarations being omitted from the JS when the experiment is compiled to JS. The solution: ensure all your JS comments begin with //, not #! Javascript comments require // in front of your comment text, not #.

It’s easy to mix this up because the Psychopy syntax highlighting misleads you into thinking that your JS comments with # are correctly interpreted as comments. When I’ve done this in the past, it’s led to variables not being declared prior to a routine. @jon it would be nice to have syntax highlighting in the JS window of the code component to be consistent with JS rather than Python, but I understand the developers have their priorities!

All that said, I’m not completely convinced this is the full extent of your issue because I’d expect an error that would lead to your experiment crashing rather than continuing with unexpected behaviour.

@kevinhroberts
Apologies! I had renamed the project. Does this link work?

It seems to work now when I run it as an experiment via pavlovia but did not when I ran it online form the builder. I’m not sure what causes this
Thanks for your suggestions anyway!