Range function with two parameters on Pavlovia

Hi everyone:

I have now a problem with range() online.
Notice that I already have this in code_js in my first routine.

// we use range() in python so make equivalent in JS
function range(size, startAt = 0) {
    return [...Array(size).keys()].map(i => i + startAt);
}

It works fine when I put only one parameter like: list(range(18)), but fails to work when I have two.
In my experiment I use range() to decide the range of rows to select. In the offline version, I give range() a start and an end number and works perfectly:

# define row start and end
study_row_start = study_row_number * blockSequenceLength
study_row_end = study_row_start + blockSequenceLength - 1
study_row_selected = list(range(study_row_start, study_row_end + 1))

I noticed that different from range() in python, this JS range() needs “size” and “startAt”, so I modified the above code to:

# modify range() accordingly
# define row start, and blockSequenceLength is the needed range size
study_row_start = study_row_number * blockSequenceLength
study_row_selected = list(range(blockSequenceLength, study_row_start))

I tested this on Start learning JavaScript with our free real time tutorial and can get what I want without a problem. However, with this, when I ran it online it still showed “range is not defined” when reached this part (I checked in the developer tool it worked fine for range(18) as I said); when I ran locally, it failed to select rows but would just loop through a whole list of stimuli.

Any help will be appreciated.

Personally I’ve found the best way to select a range of rows in a loop is

str(study_row_start-1)+“:”+str(study_row_end)

Hello,

but

PsychoJS does not matter off-line. Off-line only the python-part is of relevance. Am I correct in assuming that there are two errors?

Error 1: range is not defined when you try to define the second set of to be selected rows and run the experiment off-line?
Error 2: when run locally it does not select the intended rows?

Well, as said above Error 1 and Error 2 might be unrelated because different code is used off-line and on-line. You also might define the range-command wrong when selecting rows for the second time. Do mind to show us the relevant components and code-elements? It might be good to address one problem after the other.

Best wishes Jens

Hi wake, should I put this “$str(study_row_start-1)+”:“+str(study_row_end)” directly in selected rows in the GUI, or should I assign it to a variable (say “rows”) and put “$rows” there?
Personally I find we cannot do “$row_start:row_end” in GUI but could do:
rows = [row_start, row_end], and then assign $rows.

Ok I tried: study_row_selected = str(study_row_start)+":"+str(study_row_end+1)
Then this variable works perfectly selecting rows offline and solved the range() issue online for now. Thanks so much as always!

2 Likes

Hi Jens–thanks for your quick reply. My EXP flow and code is rather complicated so I won’t waste your time here. I used the way wake suggested above to select rows now it works perfectly. My guess is it is still the way I use range() online is incorrect–how to use range when I don’t want to start with 0, say I need [4, 5, 6, 7]. Thanks anyway!