| Reference | Downloads | Github

JS code that is able to use results from a previous trial

URL of experiment:

Description of the problem: In trial 4 of my experiment, it needs to check what was answered in trial 3 AND what was answered in trial 4 in order to essentially mark a score. For some reason, my code cannot see the trial 3 info. It only performs the subsequent step if the condition after the and (&&) is true. It ignores the first step. FYI trial 3 and trial 4 are also not in the same loop. I’m not sure if that is having an effect. Here is my code:

if (FOK_Click.clicked_name == "YesBox" && mouse_recog.clicked_name == CorrectLure) {
    num_YY = num_YY + 1;
else if (FOK_Click.clicked_name == "YesBox" && mouse_recog.clicked_name != CorrectLure) {
    num_YN = num_YN + 1;
else if (FOK_Click.clicked_name == "NoBox" && mouse_recog.clicked_name == CorrectLure) {
    num_NY = num_NY + 1;
else if (FOK_Click.clicked_name == "NoBox" && mouse_recog.clicked_name != CorrectLure) {
    num_NN = num_NN + 1;

I separated the code to check the code. I put this code in trial 3:

if (FOK_Click.clicked_name == "NoBox") {
    FOK_Choice = 0;

if (FOK_Click.clicked_name == "YesBox") {
    FOK_Choice = 1;

psychoJS.experiment.addData('FOK_Choice', FOK_Choice);

That code is recording correctly. And, I put this code in trial 4:

if (mouse_recog.clicked_name == CorrectLure) {
     corrrecog = 1;
else {
     corrrecog = 0;

That code is recording correctly. So, it’s not the code.

I’ve tried several other things (moving the code to end of experiment, adding trial_3. to the start of FOK_Click.clicked_name, moving the add data code to beginning of trial 4 - which shows as all 0s when I do that, etc) and nothing has resulted in the and statements recording correctly. Regardless, the separated statements do record correctly.

So, what code do I need to get my trial 4 code to see the results that happened in trial 3?

Thanks very much for any help you can give me!

So, I just tried adding this to the beginning of trial 4:

psychoJS.experiment.addData('box', FOK_Click.clicked_name)

In trial 3, FOK_Click.clicked_name records properly as to whether I clicked the yes or no box. I get YesBox or NoBox in the excel cell.

In the box column that I added above in trial 4, I get [“YesBox”] in every cell. So, I think the problem is that clicked_name is an array. So, it is only pulling the first item in the array. I don’t know how to solve that. Particularly because trial 4 is randomized. So, even if I could pull the full array, the items would be in a different order from the items in trial 4.

I’ve tried nesting everything in the same trial, hoping that would link the trials so it could record properly:

I’m still having the same problem, that didn’t change anything. I tried playing with checking and unchecking the is trials box in the various trials. That did not work either. Do I need a staircase loop? If so, can someone help with that, as I don’t understand from the book how to apply it to this experiment. Thanks again!

Ok, so I figured out a very inelegant way to create an array for trial 3 and create an array for trial 4 and then compare the two arrays. I’m sure there’s a better way to do this, but the code I’m using works in both Python and JS.

The problem is that trial 4 is supposed to be randomized, so the code technically doesn’t work as I’m not comparing the same item between the trials. I’ve played with staircase loops on the demo, and don’t think that will work. So, I’m wondering is it even possible to do what I need:

Get results in a randomized looped trial, and then use those results in a later randomized looped trial as a condition along with a condition from the second randomized looped trial to come up with total scores?

It may not be possible, but either way, I would love to know.


Hi @wineandsushi, if you explain your method in more detail (like in a methods section) you might get a greater response because there will be greater clarity in what you are trying to achieve. Its a bit confusing at first, since trial 3 and trial 4 are actually loops, not trials.

Thanks! I apologize it’s not clear. I was worried about writing too much. So, I’m doing a traditional Feeling of Knowing task.

  1. The first loop presents 40 word pairs that are not semantically connected (e.g. broccoli - hamstring). Participants are asked to memorize the 40 word pairs. Each pair is presented for 4 seconds.

  2. The second and third loops present the first word (cue) and ask participants to type in the missing word that was paired with the cue word (target; e.g. broccoli - ?). If the participant types in the correct word, it moves on to the next pair. If the participant is incorrect, then they are presented with a screen [trial 3] that asks whether or not they would recognize the target word from among a choice of 4 words. The participant clicks on yes or no. Clicking on yes or no is the feeling of knowing (FOK) choice. It’s kind of like tip of the tongue phenomenon - you are either sure you’d recognize the word if you saw it, or you’re sure you have no idea what the word is and won’t recognize it.

  3. The last loop [trial 4] presents participants only with the cue words for which they were unable to recall the target word. The cue word is presented with 4 choices. The participant needs to click on the correct target word from among the 4 choices.

I’m trying to get PsychoPy to calculate when someone thought they would recognize a target word and did recognize it (YY), when someone thought they would recognize a target word and didn’t (YN), when someone thought they would not recognize a target word and did recognize it (NY), and finally, when someone thought they would not recognize a target word and did not recognize it (NN).

The problem is that the last loop is randomized. So, cue words are not presented in the same order as they are presented for the FOK choice.