Hi everyone!
I’m working on an experiment in builder to put online but Pavlovia does not like some of my codes. The error I get is this:
ReferenceError: len is not defined.
I’ve been using code to split up my sentences into separate words. The words appear one by one on the screen, then a picture appears, then a letter and participants have to answer ‘f’ or ‘j’. This can be at the end of the sentence or in the middle, which means I have two lists: SentenceBefore and SentenceAfter.
In the beforeloop I have this code added in nReps$
len(SentenceAfter.split())
In BeforeStimSentence I have this code in Begin Routine:
if BeforeLoop.thisN == 0: # only on the first iteration
SentenceBefore_list = SentenceBefore.split()
# this breaks your sentence's single string of characters into a list of individual
# words, e.g. 'The quick brown fox.' becomes ['The', 'quick', 'brown', 'fox.']
and this in the text box in BeforeStimSentence:
$SentenceBefore_list[BeforeLoop.thisN]
Does anyone know what I can do to make Pavlovia happy? Or to get it running in another way online? I apologise if I missed some crucial information - as you can guess I have no experience with coding.
I think the issue you are having is that you are using python code in javascript and it doesn’t translate well. The auto-translater for custom code components does a lot of things well but it would be very time consuming for them to cover everything and probably len() is one of the things not covered. Javascript translations for the code you mentioned would look like: (I think… I am not a javascript expert)
python:
if BeforeLoop.thisN == 0:
SentenceBefore_list = SentenceBefore.split()
javascript:
if (BeforeLoop.thisN === 0) {
SentenceBefore_list = SentenceBefore.split(" ");
}
So in general you need to make sure your javascript in your custom code components are syntactically correct and edit them as needed. You can edit them by going to the dropdown menu in the component which says Auto -> JS and changing it to Both so that you can edit the JS. Be careful because every time you open the component (or at least), the menu will revert back to Auto -> JS again and if you edit something, it will revert back to the old incorrect version.
Thank you for your quick and helpful reply! I implemented it yesterday and asked some of my colleagues to do the same. It did not run the experiment on Psychopy, and it gave me this error (I added it to my practice loop):
I hoped it would work on Pavlovia nevertheless, but now Pavlovia is forever stuck on “initialising experiment”. This happened before, when I implemented a ‘go to this webpage’ code, which after I deleted, worked. I’m trying to find another way to translate this code:
len(SentenceAfter.split())
Which I think might be the root of all problems. If you have another suggestion, please let me know. Thank you for taking the time to help me!
I looked at the javascript on Pavlovia an it said
''unexpected ;", so I removed the ‘;’ at the end of ‘SentenceAfter.split(" ").length;’
and now the experiment runs but does not like the loops and now there’s a new error.
core-2020.1.js:1437 TypeError: Cannot read property '4' of undefined
at Scheduler._currentTask (Experiment.js:1916)
at Scheduler._runNextTasks (util-2020.1.js:1091)
at Scheduler._runNextTasks (util-2020.1.js:1094)
at Scheduler._runNextTasks (util-2020.1.js:1094)
at Scheduler._runNextTasks (util-2020.1.js:1094)
at update (util-2020.1.js:1058)
The SyntaxError in your python code is there because it seems you used the javascript translation I gave you directly into your python code. That translation will only work on the javascript side (modulo the semicolon error you found, semicolons are only to mark the end of a line of code in js). So I would revert the code to what it was before on the python (left) side (i.e. len(SentBeforePrac.split()) and then click the dropdown menu for Code Type and select Both. Then edit the right hand side to change the javascript. This should allow you to run both in python locally and javascript online.
Also, the S in .Split() should be lowercase (.split()). I’m fairly certain that JS is case-sensitive so it won’t recognize the split method if letters are in the wrong case.
Unfortunately I don’t know about the TypeError you show here. Sorry I can’t help you much there!
Thanks again for your answer and for taking your time to help me.
The problem is that the len(SentBeforePrac.split()` ) is not in the custom code menu as you describe. It’s in the Loop properties, as shown in the image below. Therefore, I cannot change it to both, as for as I’m aware? Or do you perhaps know of a way to modify the code in the raw code?
In this case, create a code component before this loop starts (but after SentBeforePrac is defined and in the state you want it) and create a variable in the code component with this code. That way you can define it in both python and javascript
Okay, I think you’re very close to cracking it! I implemented your changes and it worked smoothly in Psychopy which is a good sign. In Pavlovia, I got a new reference error (this time $SentBeforePrac is not defined) which is progress nonetheless
I looked at some of the other fora where they said that you have to remove the dollar sign. That gave me the Type error again.
I made my experiment public on gitlab here if you have the time to look at it…
If not, there might be another internal issue with my experiment and you at least solved my first issue! I will mark it as solved then
I took a look through your code and don’t see anything obvious that would create the type error you are getting. I think you were right to remove the $ sign, my mistake.
The only thing I could guess (really grasping at straws) is that you have too many experiment versions in your html folder. Maybe if you remove Experiment-0.1.js and Experiment_EliseAlbertsJS.js (and their legacy browser counterparts). I wonder if the Experiment-0.1.js file in particular has an overlapping name with one of the psychojs files that server uses to run the code. That’s my best guess and it’s probably wrong! If that doesn’t work, I would suggest making a new issue about it and getting the attention of one of the developers.
Okay, I’ll make a new topic for that! Deleting the files didn’t work no. I’ll mark your previous comment as the solution and thank you again for your time and help!