Question

I have an array of words - my stimuli - and they are presented on the screen. Yet, each word has another "condition", that is they are either, say categoryA, categoryB, or categoryC. This is probably simple, but I couldn't find an answer and am stuck with it. My final aim is to assign the categories randomly to the stimuli each time the script is run.

var stim = [ "AEROPLANE", "TRAIN", "CAR", "BIKE", "WALKING"];

Here I'd like AEROPLANE to have categA, TRAIN categB, and the rest categC. I thought about something like this (or with integers instead of Letters):

var stim = [ ["AEROPLANE", A], ["TRAIN", B], ["CAR", C], ["BIKE", C], ["WALKING",C] ];

But this doesn't work. If I have categories, how would I access them to code responses?

This is the script that presents the stimuli (on each keypress a new one):

$(function(){
    $(document).keypress(function(e){
        if ($(e.target).is('input, textarea')) {
            return;
        };
        if (e.which === 97 || e.which === 108) {
            new_word = w_stim[Math.floor(Math.random() * stim.length)];;
            $("#abc").text(new_word);
        };
        });
});
Was it helpful?

Solution

Make an array of objects:

var stim = [ 
    {name:"AEROPLANE", category:"A"}, 
    {name:"TRAIN", category:"B"}, 
    {name:"CAR", category:"C"}, 
    {name:"BIKE", category:"C"}, 
    {name:"WALKING",category:"C"} 
];

Then access the objects like:

   stim[i].name
   stim[i].category

JS Fiddle: http://jsfiddle.net/pJ6X2/

OTHER TIPS

Another option would be

var stim = {
  'A': ['AEROPLANE'],
  'B': ['TRAIN'],
  'C': ['CAR', 'BIKE', 'WALKING']
}

var items = stim[categoryChar];
if (undefined === items)
  console.log('no such category');
else
  console.log(items[Math.floor(Math.random() * items.length)]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top