# Performance criterion translated to Java Script

**URL:** <https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865>\
**Category:** Online experiments\
**Created:** [May 21, 2019, 6:19pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865 "2019-05-21T18:19:56Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![MartaT](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/martat/32/3758_2.png) [@MartaT](https://discourse.psychopy.org/u/MartaT)\
**Post date:** [May 21, 2019, 6:19pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865/1 "2019-05-21T18:19:56Z")

</div>

**Task URL** [https://pavlovia.org/run/Topor/probabilistic-learning-task/html/](https://pavlovia.org/run/Topor/probabilistic-learning-task/html/)  
**Description of the problem:** The task is still quite rough around the edges. I am focusing at the moment on translating the code from Python to PsychoPy.

The task has three phases. The first one is a practice phase and it works fine. The second one is a learning phase and this is where a lot of code has been added. I made a threshold that each participant has to reach before they can progress to the last phase of the experiment. I have already translated parts of the code so the task is working to a certain point. As soon as I started translating the code for the performance criterion, the task stopped working. When I run it, a message shows up “loading the experiment”. I saw on other threads that this means the code is not translated correctly.

This is what I did to establish the criterion

_first set of stimuli_  
_sum of correct responses in the last 60 trials in conditions 1 \> 12, in conditions 2 \> 11 and in condition 3 \> 9, there must be at least 60 trials in the training phase_

```auto
if myCount == 1 and (sum(resplist1[-60:]) > 12 and sum(resplist2[-60:]) > 11 and sum(resplist3[-60:]) > 9 and myCount1 > 59):
    practice.finished = True

```

I understand that Java Script does not have options to easily operate on arrays.  
I have created array slices to identify the last 60 trials, and I made a function to sum the array contents. It looks like this:

```javascript
practiceA = resplist1.slice(-60,)
practiceB = resplist2.slice(-60,)
practiceC = resplist3.slice(-60,)

arrSum = function(arr){
  return arr.reduce(function(a,b){
    return a + b
  }, 0);
}

Corr1 = arrSum(practiceA)
Corr2 = arrSum(practiceB)
Corr3 = arrSum(practiceC)

if (myCount == 1 and Corr1 > 12 and Corr2 > 11 and Corr3 > 9 and myCount1 > 59)){``
   practice.finished = true;
}

```

The empty arrays are stated in the `Begin routine` tab.  
However, only in the `End Routine` tab I included a code that commands how the resplist arrays (used in the `Begin routine` tab) should be appended. This was not an issue in PsychoPy, but potentially an issue for JS?:

```javascript
if(resp.keys == letterA) {
  resp1 = 1;
} else {
  resp1 = 0;
}

if(resp.keys == letterC) {
  resp2 = 1;
} else {
  resp2 = 0;
}

if(resp.keys == letterE) {
  resp3 = 1;
} else {
  resp3 = 0;
}

resplist1.appendChild(resp1)
resplist2.appendChild(resp2)
resplist3.appendChild(resp3)

```

I will appreciate it if someone can look through the code to see if there is anything obvious that could be wrong. The task overall has more code so it is possible that the pieces are in the wrong place as well.

I will appreciate all help!

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [May 22, 2019, 4:21pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865/2 "2019-05-22T16:21:06Z")

</div>

@MartaT, the first thing I can see is the use of `and` in your conditional if statements. In JS, you should use `&&` instead. Regarding resplist1 to 3, which look like arrays, I would use the `push` method to add to those arrays, although this will give nested lists if your push an array to the array, rather than pushing an int/float/string. Alternatively, you can use the `concat` method, which flattens out the array if you try to concatenate an array to an array. Try those fixes first, and then we see if any other errors are raised.

---

<div class="post-metadata">

**Author:** ![MartaT](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/martat/32/3758_2.png) [@MartaT](https://discourse.psychopy.org/u/MartaT)\
**Post date:** [May 23, 2019, 1:41pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865/3 "2019-05-23T13:41:30Z")

</div>

Hi @dvbridges I have made the suggested changes. So my performance criterion code now looks like this:

```auto
if (myCount == 1 && Corr1 > 12 && Corr2 > 11 && Corr3 > 9 && myCount1 > 59)){
    practice.finished = true;
}

```

And the resplists1-3:

```javascript
resplist1.push(resp1)
resplist2.push(resp2)
resplist3.push(resp3)

```

However, resplists1-3 are composed of integers (I would have thought), every trial there is one new integer added - either 1 or 0.  
Then I take a slice of the last 60 numbers and create a sum in the new variable Corr1-3.

The issue does not seem to be fixed. The message “loading experiment” comes up. However, if I remove the code for performance criterion, the task loads ok.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [May 23, 2019, 1:52pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865/4 "2019-05-23T13:52:35Z")

</div>

Ok looks like the same syntax error, but the reason is because there is extra closing brackets on the following conditionals (line 1263)

```auto
if (myCount == 1 && Corr1 > 12 && Corr2 > 11 && Corr3 > 9 && myCount1 > 59)){
      practice.finished = true;
  }
  
  if (myCount == 2 && Corr1 > 12 && Corr2 > 11 && practiceCSum > 9 && Corr3 > 59)){
      practice.finished = true;
  } 
  
  if (myCount == 3 && Corr1 > 12 && Corr2 > 11 && Corr3 > 9 && myCount3 > 59)){
      practice.finished = true;
  }

```

Remove the extra closing bracket and resync.

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [May 23, 2019, 3:26pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865/5 "2019-05-23T15:26:58Z")

</div>

Marta, to format code in your post you need **3** backticks, but just at the start and end of the code like this;

```auto
here is a line
here is another

```

You can also specify the language (although I think it tries to detect that automatically too) and that alters teh keywords that get highlighted:

```python
this is Python

```

```javascript
this is JavaScript;

```

Click on edit to see how these were written. I’ve edited your first post to show you.

thanks

---

<div class="post-metadata">

**Author:** ![MartaT](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/martat/32/3758_2.png) [@MartaT](https://discourse.psychopy.org/u/MartaT)\
**Post date:** [May 23, 2019, 3:28pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865/6 "2019-05-23T15:28:16Z")

</div>

Thank you! Just noticed the changes, good stuff!

---

<div class="post-metadata">

**Author:** ![jon](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jon/32/22_2.png) [@jon](https://discourse.psychopy.org/u/jon)\
**Post date:** [May 23, 2019, 3:28pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865/7 "2019-05-23T15:28:54Z")

</div>

Regarding problems with your actual code, the lines `practiceA = resplist1.slice(-60,)` look like they need semi colons to me.

In general, your browser should have a way to “inspect” the code when it fails so you can see where potential errors are. It will probably highlight a syntax error on or just after the line that was broken.

---

<div class="post-metadata">

**Author:** ![MartaT](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/martat/32/3758_2.png) [@MartaT](https://discourse.psychopy.org/u/MartaT)\
**Post date:** [May 23, 2019, 5:38pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865/8 "2019-05-23T17:38:02Z")

</div>

@dvbridges Thanks! I removed the bracket and it removed the problem!

@jon Thank you, I read some forums about this and decided to get rid of the comma and put nothing else and it worked.

The performance criterion is working well now. I have tested it under different conditions and it seems to be working just like the original task with the python code.

I tried to see if I can finish the task without any further errors though, and I had something unexpected pop-up.

The task runs the main loop three times with different stimuli used every time. Each run includes a new learning phase with the performance criterion used and then a testing phase, which is more straightforward. No criterion and no feedback.  
The only code I left in the testing phase is the jitter specification. When trialing the task, in the very final run (run 3) just before the testing phase was supposed to start (after I pressed the space bar on the instruction screen) this happened:

![image](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/5/6/561b2cce4e2bbbfa60dfa04decb7f876defd92ee.png)

There is no ‘#’ in the js code, I know that comments are made with `//`. I checked the stimuli files that are used and there are no odd #s creeping around. The component fields also look clear from #s. Not sure where else to look or where the source of this could be.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [May 23, 2019, 5:57pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865/9 "2019-05-23T17:57:08Z")

</div>

@MartaT, I have seen this error before where the user had empty columns in their conditions file (see below). The fix was to delete the empty columns. Does this help?

> [@Online experiment issue: TypeError message](https://discourse.psychopy.org/t/online-experiment-issue-typeerror-message/7709):
>
> URL of experiment: [https://pavlovia.org/run/vanlou22/wordjumbler/html/](https://pavlovia.org/run/vanlou22/wordjumbler/html/) Everything runs perfectly on my home computer but this error message comes up when the study is run online: Unfortunately we encountered an error: TypeError: Cannot assign to read only property ‘undefined’ of object '#' Try to run the experiment again. If the error persists, contact the experiment designer. Thank you for any help in advance!

---

<div class="post-metadata">

**Author:** ![MartaT](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/martat/32/3758_2.png) [@MartaT](https://discourse.psychopy.org/u/MartaT)\
**Post date:** [May 23, 2019, 10:11pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865/11 "2019-05-23T22:11:46Z")

</div>

Brilliant! I found the bug in one of the stimuli files (and it was an empty column indeed) and it is all sorted now! I also had to clean out the cached files in the browser before it was running ok.

One final thing (I hope) I wanted to check is whether the main loop is terminated as instructed, so after three runs of the learning and testing phases subsequently.  
Unfortunately, this is not the case. I am not sure why the code for the loop to be finished is not being executed. I thought it was in the wrong place so eventually I tried it everywhere throughout the loop and it made no difference.  
The myCount variable is used by other commands for the task and that seems to be working fine so it looks like the object is updated properly. Just the loop does not terminate after 3 runs. The code looks like this:

```auto
if(myCount > 3) {
  letterMaster.finished = true;
} 

```

---

<div class="post-metadata">

**Author:** ![MartaT](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/martat/32/3758_2.png) [@MartaT](https://discourse.psychopy.org/u/MartaT)\
**Post date:** [May 24, 2019, 4:32pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865/12 "2019-05-24T16:32:13Z")

</div>

@dvbridges  
I see that this issue is being worked on as per the github thread.

> <https://github.com/psychopy/psychojs/issues/34>
>
> The desired behaviour is to break the current loop when \`\`\`loop.finished === tru…e\`\`\`, so no more trials in that loop are presented.

If there are no updates on this I will just set up the task as a pilot with the number of possible stimuli equal to the number of loops. I will keep an eye on the thread for when this is changed to update the task.

Just to note though, the performance criterion itself has a condition to terminate the practice loop and this works.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [May 25, 2019, 1:22pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865/13 "2019-05-25T13:22:50Z")

</div>

Hi @MartaT, this fix was made and released in 3.1.0, so if you are using version \>= 3.1.0 the loop finishing statement should work (I have tested this in 3.1.2 standalone and it does work). What this will not do is finish your routines that are in the loop. All routines will complete before the loop finishes. In that case, if I only wanted 3 iterations, and my counter starts from zero, but I am incrementing my counter before checking whether the loop should end (which I think is what you are doing), I would check for equality instead:

```javascript
myCount = myCount + 1
if (myCount === 1) {
    currentLoop.finished = true; // finish at end of this loop
}

```

---

<div class="post-metadata">

**Author:** ![MartaT](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/martat/32/3758_2.png) [@MartaT](https://discourse.psychopy.org/u/MartaT)\
**Post date:** [May 27, 2019, 3:43pm UTC](https://discourse.psychopy.org/t/performance-criterion-translated-to-java-script/7865/14 "2019-05-27T15:43:24Z")

</div>

Hi @dvbridges thanks a lot for explaining this. I have tried to go with equality before but it didn’t work although I only used 2 equal signs. I trialled the task again today a few times with triple equals and still the loop does not terminate.

I was worried that the task maybe does not update properly between each sync so I made sure to export the html manually and I also tested the task on a different computer today. I have no idea what might be the issue.
