| Reference | Downloads | Github

Random is not defined

URL of experiment:

Description of the problem: I am currently getting an error message, “Random is not defined”. I know that this something that has to do with javascript, but am having trouble identifying what it is. I looked through my javascript code, and it looks like all the instances are defined, but I am new to javascript.

I used this function to shuffle:

  // create a function to shuffle a list
function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    return a;

From my understanding, it is okay in how it uses Math.random.

Separately from this, but related, I was wondering which file is the best to edit when I make changes to the javascript (after syncing my experiment). Within the html folder, there is the legacy-browsers.js file and the “.js” file. Should I be editing both of these?

thank you for all your help,

That looks correct for a shuffle function. Your repository is currently private so I cant see if there are other usages of “random” that are causing trouble, but I’m pretty confident there are, because it would say something like “math has no function random” or something like that if this were the main issue. If you make your repository public I can have a quick look. (In the code repository, go to general settings -> permissions.)

Editing the JS after uploading is tricky. If you resync your project it will overwrite any edits you’ve made to the html directly because it re-generates the js file every time. However, the main difference between legacy-browsers and the other js file, as I understand it, is that legacy-browsers is for things like Microsoft Edge or very outdated versions of other browsers.

Thank you for the information about changing the permissions. I made the repository public, and would love to hear your feedback about the error.

Also, thank you for clarifying the difference between the two html files.

The problem seems to be in the “begin routine” code component of the “fileController” trial type, which contains the following:

    Max_Squares_1 = random.randint(1, 8);
    Max_Squares_2 = ((10 - Max_Squares_1) - 1);
    Max_Squares_2 = random.randint(1, Max_Squares_2);
    Total_Squares = (Max_Squares_1 + Max_Squares_2);
    Squares_3_Num = (10 - Total_Squares);

random.randint is a python function, and I’m 99% sure it’s where the “random is not defined” error is coming from.

This should be what you need instead

    Max_Squares_1 = Math.floor(Math.random()*7)+1;
    Max_Squares_2 = ((10 - Max_Squares_1) - 1);
    Max_Squares_2 = Math.floor(Math.random()*(Max_Squares_2-1))+1;
    Total_Squares = (Max_Squares_1 + Max_Squares_2);
    Squares_3_Num = (10 - Total_Squares);

Starting from end-of-range minus 1 and adding one to the end result is because Math.random always includes 0 in its range, so the range of Math.floor(Math.random()*7) will be 0-7, and you want 1-8.

Thank you for taking a look at this and for the suggestion.
I implemented this change, but am now getting an error which states that, “shuffle function is not defined”.

I moved the shuffle code (which I posed in my initial question) to the beginning of the “Begin Routine” section of the code, but that doesn’t seem to fix the error.

I changed the name of this updated version to: Oddball_Testing_Attempt_81

I would love to hear if you have any suggestions. Really appreciate your help with this.

Oddball_test_81 is not public, you will need to make it public as well.

Did you move it to the same code component (begin routine of the fileController trial type), and put it before it’s called? That should definitely do the trick.

Oddball_Testing_Attempt_81 is public (Oddball_test_81 is a different experiment). My appologies for the confusing names.

I did move the code component to the Begin Experiment, which in my understanding should preceed Begin Routine, and also preceeds some functions that call it.

Ah, this is a javascript scope issue. Putting a function in “Begin experiment” doesn’t make it globally available to the experiment, because “begin experiment” is itself part of a function. The easiest way to do it is to define the function immediately before you use it, in the same code component.

Thanks for the suggestion!
What I did is place the function at the beginning of “Begin Experiment” as well as at the beginning of “Begin routine” to make sure that the scope reaches everything.
However, it is now telling me: " * TypeError: Cannot read property ‘length’ of undefined

I often say progress is failing differently. In this case, I think it’s telling you that “conds” is not defined.

That is a brilliant saying indeed :slight_smile:
If you don’t mind clarifying this point:
Within the “Begin Experiment” tab, I declared “conds” by writing var conds;
I then assigned stuff to it at the end of that same tab:
conds = [];

My understanding is that if you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable. Is the issue then, that I should not be declaring it in the first place, in order to allow the “Begin Routine” to access it?

Thank you for all of your help with this.

Remove the var conds line from that. If it is preceded by a “var” statement and not at the top level of the file, it will not be treated as global.