ReferenceError: answer is not defined

URL of experiment: Pavlovia

Description of the problem:
Hi, I ran into the error of ReferenceError: answer is not defined showed up in the main experiment but not in the practice session. I compared the response properties in both sessions by checking the correct answer. They should work fine, I am not sure which part of the code causes this answer error. Thanks for the help in advance.

This tutorial might help you find the issue: Thomas Pronk / tutorial_js_semantic_error · GitLab

thanks, @thomas_pronk for your reply.

See the image below for the source code for the error line 2989 ‘console.log’ (answer, correctAns). Because this experiment was created with the builder, I presume that default parameters such answer key corr rt don’t need to be initiated in the code component. The answer from the participant’s response will be utilized to match with the $correctAns to get the respKey.corr output, as seen here.
It appears quite unlikely that defining answer in the BeginRoutine will fix the bug, maybe I miss something important here. :confused:

What do these variables do according to you?

Hi thank you for your reply. I am not sure which variables do you refer to? I guess you refer to the variables in console.log. Aside from the variable ‘answer,’ I’m having trouble finding it in my experience. (builder), other variables I can find the meaning from here: Builder - providing feedback — PsychoPy v2021.1.

OK, let me recap. You’ve shown me a couple of lines of JS. In those lines I see some variables and I see the console.log statement. That statement prints the values of variables to the browser console, as illustrated in this tutorial. They don’t actually affect your experiment. I don’t see any corrAns in the page you linked though, but one way or the other these lines of code ended up in your experiment. I’m wondering: how did they end up there? What do you think is their function?

sorry, you are right that I did not explain where did the correctAns come from. Thanks for pointing it out. This variable is defined in the Block1.xlsx file (on the right panel in the image) that I use to load images and store the correct answer for this block (on the left panel, you can find the Block1.xlsx for the conditions). In Block1.xlsx there is no variable named answer. According to the error message points to line 2989 in the concole.log statement, it seems the error comes from the resp routine where respKey has been defined. But look at the previous screenshot, there is also no variable named answer. The answer seems to store the output from correctAns in the console.log, what do you think?

Based on what I heard, this problem is too complex for me to solve for you. I would go take a step back, and build it up your experiment from scratch so you know exactly what is happening. Test it frequently as you build, so that when a bug happens, you’ve got a better grasp at where this might have gone wrong.

1 Like

thanks for the tips, I will post the answer once the problem is fixed :slight_smile:

Thanks. Good luck!

I think that the issue may be that in some of your cells of your Block1.xlsx and Block2.xlsx spreadsheets you have a formula (=IF(AND(OR(D107="Fear",P108="eyes"),P108="mouth"),"b","m") for your correct answer instead of a letter. This isn’t the case for practice_v1.xlsx so it’s worth a try. You can copy correctAns columns to a new column and then copy and paste as values back to the original.

1 Like