# Failed to parse as JS; code component for customised mouse response causing errors

**URL:** <https://discourse.psychopy.org/t/failed-to-parse-as-js-code-component-for-customised-mouse-response-causing-errors/8247>\
**Category:** Online experiments\
**Created:** [June 25, 2019, 3:19pm UTC](https://discourse.psychopy.org/t/failed-to-parse-as-js-code-component-for-customised-mouse-response-causing-errors/8247 "2019-06-25T15:19:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jretz](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@jretz](https://discourse.psychopy.org/u/jretz)\
**Post date:** [June 25, 2019, 3:19pm UTC](https://discourse.psychopy.org/t/failed-to-parse-as-js-code-component-for-customised-mouse-response-causing-errors/8247/1 "2019-06-25T15:19:41Z")

</div>

URL of experiment: [https://gitlab.pavlovia.org/jenny.retzler/memory\_span](https://gitlab.pavlovia.org/jenny.retzler/memory_span)

My experiment runs fine in builder and has some code components. I think it is my poor attempts at translating these to JS that is causing the error ‘failed to parse as JS by esprima’. The files upload to Pavlovia but the study gets stuck at the ‘initializing experiment’ screen. I suspect the problem is in the ‘Each frame’ block of the code associated with one routine and may relate to mouse.getPressedIn, but despite finding some relevant threads and looking at examples such as the Corsi blocks, I’ve been unable to solve the issue.

Begin routine (Python):

```auto
pracCount=pracCount+1
squares=[RSq1,RSq2,RSq3,RSq4,RSq5,RSq6,RSq7,RSq8,RSq9,RSq10,RSq11,RSq12,RSq13,RSq14,RSq15,RSq16]
clickSequence=[]
clickedSquare =[]
clickedSquareNum =[]

```

Begin routine (attempted JS equivalent):

```auto
pracCount=pracCount+1;
squares=[RSq1,RSq2,RSq3,RSq4,RSq5,RSq6,RSq7,RSq8,RSq9,RSq10,RSq11,RSq12,RSq13,RSq14,RSq15,RSq16];
clickSequence=[];
clickedSquare =[];
clickedSquareNum =[];

```

Each frame (Python):

```auto
for square in [RSq1,RSq2,RSq3,RSq4,RSq5,RSq6,RSq7,RSq8,RSq9,RSq10,RSq11,RSq12,RSq13,RSq14,RSq15,RSq16]:
    if mouse.isPressedIn(square):
        #find out the square name
        clickedSquare = square.name
        #extract the number only from the square name
        clickedSquareNum = int(''.join(filter(str.isdigit, clickedSquare)))
        #avoid double-clicks and add to clickSequence
        if clickSequence==[]:
            clickSequence.append(clickedSquareNum)
        elif clickedSquareNum!=clickSequence[-1]:
            clickSequence.append(clickedSquareNum)

# all clicked?
if len(clickSequence) >= len(respSequence):
    continueRoutine = False
    event.clearEvents()

```

Each frame (attempted JS equivalent):

```auto
    if (mouse.isPressedIn(square)){
        clickedSquare = square.name;
        clickedSquareNum = clickedSquare.replace(/[^0-9]/g,@'');
        if (clickSequence===[]){
            clickSequence.append(clickedSquareNum);
        }
        else if (clickedSquareNum!==clickSequence[-1]){
            clickSequence.append(clickedSquareNum);
        }
    }
}

if (clickSequence.length >= respSequence.length){
    continueRoutine = false;
}

```

End routine (Python):

```auto
thisExp.addData('clickSequence',clickSequence)

#determine if all items recalled, order doesn't matter

if list(set(clickSequence))==list(set(respSequence)):
    correctCount=correctCount+1

```

End routine (attempted JS equivalent):

```auto
psychoJS.experiment.addData('clickSequence',clickSequence)

if (clickSequence.length === respSequence.length && clickSequence.sort().every(function(value, index) { return value === respSequence.sort()[index]})=true){
    correctCount=correctCount+1;
}

```

There are one or two other very basic code components I can provide info for if needed, but I think it is unlikely that they are the issue.

Thanks in advance for any help - sorry for the long post!

Jenny

---

<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:** [June 25, 2019, 3:52pm UTC](https://discourse.psychopy.org/t/failed-to-parse-as-js-code-component-for-customised-mouse-response-causing-errors/8247/2 "2019-06-25T15:52:35Z")

</div>

The last bit of code might be the reason. The code below is more readable, so try that and see if the error changes.

```javascript
cs = clickSequence.sort();
rs = respSequence.sort();
sameLength = cs.length === rs.length;
diff = cs.filter(function(x) { return rs.indexOf(x) < 0 }).length;

if (sameLength && diff === 0) 
{
   correctCount = correctCount + 1;
}

```

There may also be an error with your regexp. you may need to remove the @ symbol.

```javascript
clickedSquareNum = clickedSquare.replace(/[^0-9]/g,@'')

```

---

<div class="post-metadata">

**Author:** ![jretz](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@jretz](https://discourse.psychopy.org/u/jretz)\
**Post date:** [June 28, 2019, 10:50am UTC](https://discourse.psychopy.org/t/failed-to-parse-as-js-code-component-for-customised-mouse-response-causing-errors/8247/3 "2019-06-28T10:50:18Z")

</div>

Hi @dvbridges

Thanks for the response. I made both of the changes and still had the error after your first suggestion, but removing the @ has stopped the ‘failed to parse’ message and it now starts to run.

However, I am now getting this error message:

> - **TypeError: Cannot read property ‘0’ of undefined**

From other threads it looks as though this has to do with the generation and updating of new variables within the script (so perhaps clickSequence, correctCount, etc), but it is not clear how to resolve the issue.

Many thanks for your help,  
Jenny

---

<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:** [July 1, 2019, 9:17am UTC](https://discourse.psychopy.org/t/failed-to-parse-as-js-code-component-for-customised-mouse-response-causing-errors/8247/4 "2019-07-01T09:17:14Z")

</div>

Hi @jretz, I think this is related to the mouse settings and having “New clicks only” unticked. If you select “New ticks only”, the error should go away. This is a bug, and I am making the fix now.

---

<div class="post-metadata">

**Author:** ![jretz](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@jretz](https://discourse.psychopy.org/u/jretz)\
**Post date:** [July 2, 2019, 11:51am UTC](https://discourse.psychopy.org/t/failed-to-parse-as-js-code-component-for-customised-mouse-response-causing-errors/8247/5 "2019-07-02T11:51:48Z")

</div>

Perfect - that has done the trick. However, I am now seeing the following:

> Unfortunately we encountered an error:
> 
> - **TypeError: mouse.isPressedIn is not a function**
> 
> Try to run the experiment again. If the error persists, contact the experiment designer.

Again, looking on other forums it looks like this has come up before, but using that function has been suggested as a solution too ([RatingScale in online experiments](https://discourse.psychopy.org/t/ratingscale-in-online-experiments/5265)) - is this something that should be working? My clickable stimuli are slightly overlapping at the moment, so perhaps that could be causing issues. Incidentally, if I want square image stimuli for an online experiment would ‘height’ be the best unit to use?

Thank you!

---

<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:** [July 2, 2019, 1:59pm UTC](https://discourse.psychopy.org/t/failed-to-parse-as-js-code-component-for-customised-mouse-response-causing-errors/8247/6 "2019-07-02T13:59:39Z")

</div>

@jretz, the `isPressedIn` function is not available on Pavlovia. However, it looks like you have already set up your experiment to take advantage of the mouse objects valid clicks, which stores the names of the objects clicked in the mouse object. You can change your code using the following for Python and JS. Note, with the JS, I have had to explicitly define the size of your squares in the `Respond` routine, because without the size the clicks are not detected. Replace your own code with the code below.

# Python

```auto
# Each Frame
if len(mouse.clicked_name) >= len(respSequence):
    continueRoutine = False
    event.clearEvents()

# End routine
clickSequence = [int(click.replace("RSq", '')) for click in mouse.clicked_name]
thisExp.addData('clickSequence', clickSequence)

if list(set(clickSequence))==list(set(respSequence)):
    correctCount=correctCount+1

```

# The JS equivalent:

```javascript
////////// Begin Routine ////////// 

pracCount=pracCount+1;
squares=[RSq1,RSq2,RSq3,RSq4,RSq5,RSq6,RSq7,RSq8,RSq9,RSq10,RSq11,RSq12,RSq13,RSq14,RSq15,RSq16];
clickSequence=[];

// Change size of squares
// Need size to detect clicks
for (let i of squares)
  {i.setSize([.2, .4])}

////////// Each Frame ////////// 

// Convert respSequence to array
rs = JSON.parse(respSequence)

// Check number of squares clicked
if (mouse.clicked_name.length >= rs.length){
    continueRoutine = false;
}

////////// End Routine ////////// 

// Get click sequence
clickSequence = []
for (let each of mouse.clicked_name) {
    clickSequence.push(Number(each.replace('RSq', '')))
}

// Save click sequence
psychoJS.experiment.addData('clickSequence', clickSequence)

// Sort clicked and resp sequences for comparison
cs = clickSequence.sort((a, b) => a > b ? 1 : -1);
rs = rs.sort((a, b) => a > b ? 1 : -1)
sameLength = cs.length === rs.length;
diff = cs.filter(function(x) { return rs.indexOf(x) < 0 }).length;

if (sameLength && diff === 0) 
{
   correctCount = correctCount + 1;
}

```

Alternatively, here is a working version [VS\_Memory\_Seq\_Online - Copy.psyexp](https://discourse.psychopy.org/uploads/short-url/t9OIkG39ZvVPBtWvNlKi08L7hBD.psyexp) (151.2 KB)

---

<div class="post-metadata">

**Author:** ![jretz](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@jretz](https://discourse.psychopy.org/u/jretz)\
**Post date:** [July 8, 2019, 9:09am UTC](https://discourse.psychopy.org/t/failed-to-parse-as-js-code-component-for-customised-mouse-response-causing-errors/8247/7 "2019-07-08T09:09:49Z")

</div>

Hi @dvbridges . Thanks so much for the fix. It’s not quite doing what I need in terms of what it is allowing and what it is recording, but hopefully I’ll be able to make those adjustments myself with the code you’ve provided 🙂

Jenny
