Converting rowx to JS

Hi there,

I have an experiment that works perfectly offline and has some code components within. I’m now trying to get the experiment to work online; I’ve converted most of the code but I’m really struggling to find a substitute for this block of code:

#loop through the rows in the stimulus file
for rowx in range(1,num_items+1):
   
```#read in an entire row
```row = insheet.row_values(rowx)
    
```#Save the stimuli
```emo_face.append(row[0])
```neu_face.append(row[1])
```pos_poly.append(row[2])

Just to clarify, this code is part of a larger set which is attempting to access and read through an excel conditions file with some randomization taking place (i’ve used the trialhandler to access the conditions file). ‘num_items’ are the amount of items to be processed in the routine; and all the stimuli at the last section of code are the columns of stimuli within the conditions file.

I’ve attempted to use the trialList to access the stimuli instead but I can’t seem to get it to do what it previously done in Python. Basically I’m asking if there is a way to substitute ‘range’ and ‘rowx’ in Javascript? Thank you.

Hi @JackB, the equivalent ‘for loop’ in JS would be:

for (let rowx = 1; rowx < (num_items + 1); rowx++) {
  // do something
}  

Thank you very much @dvbridges, I greatly appreciate it. I just had one more question. I saw on a previous post, you wrote this line of code:

aValue = myList.trialList[0]['variableName'] // Get value from row 0

This was in reference to a codeblock using the trialhandler to access a conditions file. I just wanted to know if there’s a similar version to access a column instead, sort of like colx. Alternatively is it just possible to access/import JS libraries (e.g. npm install exceljs)?

Thanks again and kind regards,

Jack

@JackB, you can get a column of values from the trial handler after it has been created in your study. To do this, you use the map function to create a new array of those column values. In the example below, we extract all values from the text column, from a trial handler called trials:

newArray = trials.trialList.map(col => col.text)

Thanks very much for the help @dvbridges

1 Like

Hi, so you were able to figure this out? I’m essentially trying to convert the same code you were in this thread (from Jason Ozubko’s Youtube videos, perhaps?) with similar kinds of randomization and the internet is very confusing. I can’t ever tell which language responders are writing in or how the Python/JS conversions look. Does your study work online and also in builder? If so, would you mind sharing it please? TIA!