Illegal Return Statement for "return psychoJS.quit"

URL of experiment: https://pavlovia.org/run/Jordan_Gallant/megansMazeTask/html/

Description of the problem: I’m helping an undergraduate student to build a PsychoPy3 experiment and I encountered a strange issue. The PY version of the experiment runs nicely, and I was working through some bugs in my JS code when I came across this error in the console:

Uncaught SyntaxError: Illegal return statement refering to line:
/run/Jordan_Gallant/megansMazeTask/html/megansMazeTask.js:727

The line in question is the second line of the following:

if (psychoJS.experiment.experimentEnded || psychoJS.eventManager.getKeys({keyList:[‘escape’]}).length > 0) {
return psychoJS.quit(‘The [Escape] key was pressed. Goodbye!’, false);
}

I was quite confused because I checked other experiments that are currently working and the same line appears. I’m not sure why this is happening. I suspect it has something to do with the custom JS code elements in the experiment, but that’s a vague guess at best.

Any ideas?

Yes, I think the problem is your inserted code.

The error (in firefox) says SyntaxError: return not in function because the call to return must be inside some function. In your case it should be inside the mazeTaskRoutineEachFrame() function.

Then you have to think, “why is it not inside that function?”

The issue is that the function is defined by the { }. If your inserted code has an extra } somewhere then it will end the function and then return is sitting outside the function.

I’ve moved your JS file to an editor (e.g. sublime) that shows up which brackets are matching which others and tracked it down to your code here:

  if (count == 6) {
      
      if (randPos == 0) {
          currentWord = stimulus[count] + "                       " + distractor;
      } else {
          currentWord = distractor + "                        " + stimulus[count];
      }
      
  } else {
      currentWord = stimulus[count];
      }
  }

Your final ‘else’ has what looks like an extra } and so the function code has finished here.

It’s one of the tricky things with syntax errors that they often only get detected somewhere else in the code.

Thanks for your help @jon, I’m glad it’s a quick fix. I’m still getting used to the JS syntax.

I’ve been using the Chrome console to debug experiments. Do you have any other recommendations for this that might catch syntax errors like the one above. That would save me some time.