"index is not a function" error

Description of the problem:
Hello,

In my experiment I have created a list of circles and another list of letters which are located the center of related circles. What I need to do is to determine the order in the list of circle which clicked by mouse and set both circle’s and related letter’s positions at the same time. I have used below code:

#there will be 26 circles and letters
list1 = [circleA, circleB]
list2 = [textA, textB]

# mouse movement for objects in the list 
for obj in list1:
    listCount = list1.index(obj)
    if mouse.isPressedIn(obj, buttons=[0]) and objectCount == 0:
        objectCount += 1; 
        if objectCount == 1 and  mouse.getPressed() == False:
            objectCount -= 1; 
        elif objectCount == 1 or mouse.getPressed():
            x, y = mouse.getPos()
            obj.setPos((x, y), log=False)
            list2[listCount].setPos((x, y), log=False);

that code works in PschoPy perfect but when I try to run it at Pavlovia, I get " list1.index is not a function" error. As far as my little experience in PsychoPy and Pavlovia, there should be a discrepancy between Python and JS.

Do I need to use another command instead of “.index”?

Have a Google of indexOf() and findIndex()

I haven’t checked my crib sheet to see if I already have a solution for this yet.

1 Like

After your reply, I tried to write function of index on JS code like your crip sheet (like randint function) but I couldn’t achive. After that, I wrote below code for JS:

indexofsth = function(elementToFind, indexTOSearch) {
  return indexTOSearch.indexOf(elementToFind);
}

and also defined its equivalent by craeting a code component for Python like you’ve adviced in your crib sheet as below:

def indexofsth(elementToFind, indexTOSearch): 
    return indexTOSearch.index(elementToFind)

Your reply was really helpful. Thank you.

1 Like