In my experiment I have a list of locations in which an image stimulus is presented. Participants are required to press the spacebar if they see the image presented in the same location consecutively. I have coded a current location and last location, and I know this works correctly as it records that data perfectly.
I have created it so that in my csv it should tell me true or false if currentlocation = lastlocation. This works perfectly locally, but on Pavlovia it does not record correctly.
Now this is the section in which it should record if currentloc === lastloc, and for some reason online it doesn’t work. I’ve tried replacing currentloc with locs[idx] and last loc with locs[idx]-1 and that did not work either. It is coming up false when it should be true.
I’ve also added a console log in to see when it records currentloc, lastloc, and the currentloc === lastloc. The locations are recorded correctly as you can see, but even when the 2 locations are the same it still comes up as false
Hi @emily01, I think js is just not able to compare two arrays in their entirety. You could work around that by condensing both arrays to a string first, and then comparing both strings. So,
hi @ajus , thanks for the help. I’ve tried doing as you suggested and put in my endroutine tab
psychoJS.experiment.addData(“SMALLADAPT_PRESSYES”, (currentLoc.join() === lastLoc.join()));
it returns the error * TypeError: Cannot read properties of undefined (reading ‘join’)
To debug this, you could console.log()currentLoc.join() and lastLoc.join() before trying to save anything. If this does not lead to any insight there is also an alternative to check if both arrays are identical:
I’m not too familiar with python or java as this is my first time doing something like this, but is there a problem with my currentLoc and lastLoc definition? In the console log it seems to be defined well and pulling the relevant data but then when it comes to these error messages it’s always related to the definition. What does that mean? Do you know how I can fix this?
Thanks for the help
Hi @emily01, the problem was that for the very first trial/presentation (idx = 0) there is no lastLoc (i.e., lastLoc is undefined. You can replace your code
Hi @ajus , thank you so much for your help. Your solution also worked for me! I do have another question that I hope you can help with. When I put the exact same code and format into a larger experiment, it does now record the true/false correctly, however for the first record of lastLoc where it should be [“NA”,“NA”] (as it is in the smaller experiment), still come up as whatever currentLoc is. Everything is the exact same word for word but it is just slotted in as part of a much bigger experiment. Do you know why that might be or how I could fix it?
I’m not sure if this is related or if I should make a new forum post for this. But another problem I have when I insert this small experiment into my full one, I occasionally receive this error message after a couple of image stimulus presentations of the different locations. It does not happen every time I run the experiment which is what confuses me, and the number of iterations it occurs after is also different each time.
I’ve since figured out the [“NA”,“NA”] problem by console logging, it was to do with the value of the first idx starting at 30 instead of 0 for some reason, I renamed idx to index and it seems to work now, possibly a clash with another variable named idx elsewhere in the code. For the last few iterations I’ve also not had the error message, but it’s still hard for me to know if it will come back/ the root cause. I was thinking that maybe as the idx value went above the possible number of locations, but still confusing as to why it happened after different numbers of presentations each time. I can’t be sure of the starting idx for the others unfortunately.
Ah ok, understood. I guess if it consistently works now this might have been the problem. To give yourself more peace of mind, you might want to go through your js script and search for idx / index and see if at any point something happens to or with this variable that you are not aware of or that could result in undefined values.