CAn't figure out TypeError

I have an online experiment where I think I’m being very clever in creating stimulus lists in code ahead of time but am getting an error I can’t solve. I was hoping someone might be able to give me a hint.

I have two code components running in an initial routine, both as Begin Experiment.

The first:

zip = function zip() {
var args = .slice.call(arguments);
var shortest = args.length==0 ? : args.reduce(function(a,b){
return a.length<b.length ? a : b
});

return shortest.map(function(_,i){
    return args.map(function(array){return array[i]})
});

}

The second:

win=psychoJS.window;
thisExp=psychoJS.experiment;
event=psychoJS.eventManager;
shuffle = util.shuffle;
Array.prototype.append = .push;

#List of images

MaleImages = [‘013_y_m_n_b.jpg’, #note these are all the same just for the purpose of testing
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’]

shuffle(MaleImages)

FemaleImages = [‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’,
‘013_y_m_n_b.jpg’]

shuffle(FemaleImages)

#Take a random half of male and female images, to be used with the r and s names of each type

RMaleImages = MaleImages[0:17]
SMaleImages = MaleImages[18:35]

RFemaleImages = FemaleImages[0:17]
SFemaleImages = FemaleImages[18:35]

#List of names

MaleNamesR = [“Abel”, “Allen”, “Lewis”, “Linus”, “Lorne”, “Lou”, “Lyle”, “Miles”, “Milo”, “Morris”, “Moses”, “Nathan”, “Noam”, “Noel”, “Owen”, “Ronin”, “Warren”, “Will”]

MaleNamesS = [“Eric”, “Hector”, “Chris”, “Curtis”, “Kirk”, “Ted”, “Titus”, “Tucker”, “Tate”, “Terry”, “Pierce”, “Carter”, “Kipp”, “Kurt”, “Jack”, “Victor”, “Garrett”, “Zach”]

FemaleNamesR = [“Anne”, “Joanna”, “June”, “Lanah”, “Laurel”, “Lauren”, “Lois”, “Luna”, “Mara”, “Marla”, “Megan”, “Mona”, “Myah”, “Noelle”, “Norah”, “Nya”, “Renee”, “Rosanne”]

FemaleNamesS = [“Rita”, “Erica”, “Etta”, “Patty”, “Christie”, “Katie”, “Kasey”, “Petra”, “Kathy”, “Katia”, “Kate”, “Trista”, “Tracy”, “Pippa”, “Tessa”, “Tia”, “Greta”, “Yvette”]

shuffle(MaleNamesR)
shuffle(MaleNamesS)
shuffle(FemaleNamesR)
shuffle(FemaleNamesS)

#Combine names with images

MRStim = list(zip(MaleNamesR, RMaleImages))
MSStim = list(zip(MaleNamesS, SMaleImages))
FRStim = list(zip(FemaleNamesR, RFemaleImages))
FSStim = list(zip(FemaleNamesS, SFemaleImages))

#Make a r and a s set

RStim = MRStim.concat(FRStim)
SStim = MSStim.concat(FSStim)

shuffle(RStim)
shuffle(SStim)

#Adjectives

AdjectivesR = [“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “11”, “12”, “13”, “14”, “15”, “16”, “17”, “18”, “19”, “20”, “21”, “22”, “23”, “24”, “25”, “26”, “27”, “28”, “29”, “30”, “31”, “32”, “34”, “35”, “36”]

AdjectivesS = [“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “11”, “12”, “13”, “14”, “15”, “16”, “17”, “18”, “19”, “20”, “21”, “22”, “23”, “24”, “25”, “26”, “27”, “28”, “29”, “30”, “31”, “32”, “34”, “35”, “36”]

shuffle(AdjectivesR)
shuffle(AdjectivesS)

#Combine adjectives names and images

RStimAdj = list(zip(RStim, AdjectivesR))
SStimAdj = list(zip(SStim, AdjectivesS))

#Combine r and s trial stim

Trials = RStimAdj.concat(SStimAdj)
shuffle(Trials)

#Unzip into stim
Names, Images, Adjectives = zip(*Trials)

Names = list(Names)
Images = list(Images)
Adjectives = list(Adjectives)

AllTrials = [Names, Images, Adjectives]

Then names are referenced in a later routine like this:

$AllTrials[0][trials.thisN]

However I get this error:

TypeError: Cannot read properties of undefined (reading ‘0’)

I can’t figure out what’s wrong because very similar code works for me in another experiment.

Would really appreciate any ideas!

Is your loop called trials?

It is! Though I just tried changing the name of the list from Trials to ExpTrials, and I still get the error.

Try some print statements to find out if All trials is undefined upon creation, and if so which of its elements are undefined.

Edit: Aha! I think I’ve managed to print what AllTrials is and I see there are errors.

Cool. You want

AllTrials = [Names, Images, Adjectives]

and you currently have

AllTrials = [[Names, Images], Adjectives]

I think there’s an error in your zip function.

Super! I fixed it by unzipping the names and images, making them lists, before zipping them all back up. Seems to be working now! Thanks!