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?
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.
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
That is a brilliant saying indeed
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 = ;
conds.push(Pos_Dict);
conds.push(Neut_Dict);
conds.push(Neg_Dict);
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?