Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Counter balancing the presentation of stimuli within a JS programme.

    Hello all,

    I'd like to apologise in advance for utter lack of knowledge regarding all things programming. However, I could really use some help with a code I am using for part of my research (I'm a PhD Fellow in Applied Psychology) and am struggling to figure it out.

    Basically, I have managed to tweak an existing programme to meet my needs, get it hosted on a website and collecting data, but I have noticed an issue in that the categories presented at the top are not counter balanced (Online IAT). I think I have narrowed down the issue to the following piece of code (I may be wrong) and wondered if anyone could offer me any advice on what it is I need to change (Math.Random maybe?).

    Code:
    // Initialize variables, build page & data object, display instructions
    function startIAT()
    {
    	currentState = "instruction";
    	session = 0;
    	roundnum = 0;
    	
    	// default to show results to participant
    	if (!('showResult' in template))
    	{
    	    template.showResult = "show";
    	}
    	
    	// make the target or association words green
    	if (Math.random() < 0.5)
    	{
    		openA = "<font color=green>";
    		closeA = "</font>";
    		open1 = "";
    		close1 = "";
    	}
    	else
    	{		
    		open1 = "<font color=green>";
    		close1 = "</font>";
    		openA = "";
    		closeA = "";
    	}
    	buildPage();
    	roundArray = initRounds();
        instructionPage();
    }
    
    // Adds all images to page (initially hidden) so they are pre-loaded for IAT
    function buildPage()
    {
    	if (template.catA.itemtype == "img")
    	{
    		for (i in template.catA.items)
    		{
    			var itemstr = '<img id="'+template.catA.datalabel+i+'" class="IATitem" src="templates/'+template.name+'/img/'+template.catA.items[i]+'">';
    			$("#exp_instruct").after(itemstr);
    		}
    	}
    	if (template.catB.itemtype == "img")
    	{
    		for (i in template.catB.items)
    		{
    			var itemstr = '<img id="'+template.catB.datalabel+i+'" class="IATitem" src="templates/'+template.name+'/img/'+template.catB.items[i]+'">';
    			$("#exp_instruct").after(itemstr);
    		}
    	}
    	if (template.cat1.itemtype == "img")
    	{
    		for (i in template.cat1.items)
    		{
    			var itemstr = '<img id="'+template.cat1.datalabel+i+'" class="IATitem" src="templates/'+template.name+'/img/'+template.cat1.items[i]+'">';
    			$("#exp_instruct").after(itemstr);
    		}
    	}
    	if (template.cat2.itemtype == "img")
    	{
    		for (i in template.cat2.items)
    		{
    			var itemstr = '<img id="'+template.cat2.datalabel+i+'" class="IATitem" src="templates/'+template.name+'/img/'+template.cat2.items[i]+'">';
    			$("#exp_instruct").after(itemstr);
    		}
    	}
    }
    
    // Round object
    function IATround()
    {
    	this.starttime = 0;
    	this.endtime = 0;
    	this.itemtype = "none";
    	this.category = "none";
    	this.catIndex = 0;
    	this.correct = 0;
    	this.errors = 0;
    }
    
    // Create array for each session & round, with pre-randomized ordering of images
    function initRounds()
    {
        var roundArray = [];
        // for each session
        for (var i=0; i<7; i++)
        {
            roundArray[i] = [];
            switch (i)
            {
                case 0:
                case 4:
                    stype = "target";
                    numrounds = 20;
                    break;
                case 1:    
                    stype = "association";
                    numrounds = 20;
                    break;
                case 2:
                case 3:
                case 5:
                case 6:
                    stype = "both";
                    numrounds = 40;
                    break;
                
            }
    		prevIndexA = -1; prevIndex1 = -1;
            for (var j = 0; j<numrounds; j++)
            {
                var round = new IATround();
                
                if (stype == "target")
                {
                    round.category = (Math.random() < 0.5 ? template.catA.datalabel : template.catB.datalabel);
                }
                else if (stype == "association")
                {
                    round.category = (Math.random() < 0.5 ? template.cat1.datalabel : template.cat2.datalabel);  
                }
                else if (stype == "both")
                {
    				if (j % 2 == 0) { round.category = (Math.random() < 0.5 ? template.catA.datalabel : template.catB.datalabel); }
    				else { round.category = (Math.random() < 0.5 ? template.cat1.datalabel : template.cat2.datalabel); }
                }
            	// pick a category
            	if (round.category == template.catA.datalabel) 
            	{ 
    				round.itemtype = template.catA.itemtype;
    				if (i < 4) { round.correct = 1; }
    				else { round.correct = 2; }
    				
    				// pick an item different from the last
    				do 
    					{ round.catIndex = Math.floor(Math.random()*template.catA.items.length); }
    	        	while (prevIndexA == round.catIndex)
    	        	prevIndexA = round.catIndex;
            		
            	}
            	else if (round.category == template.catB.datalabel)
            	{ 
    				round.itemtype = template.catB.itemtype;
    				if (i < 4) { round.correct = 2; }
    				else { round.correct = 1; }
    				// pick an item different from the last
    				do
    	        	    { round.catIndex = Math.floor(Math.random()*template.catB.items.length); }
    	        	while (prevIndexA == round.catIndex)
    	        	prevIndexA = round.catIndex;
            	}
            	else if (round.category == template.cat1.datalabel)
            	{ 
    				round.itemtype = template.cat1.itemtype;
            		round.correct = 1;
    				// pick an item different from the last
    				do
    	        	    { round.catIndex = Math.floor(Math.random()*template.cat1.items.length); }
    	        	while (prevIndex1 == round.catIndex)
    	        	prevIndex1 = round.catIndex;
            	}
            	else if (round.category == template.cat2.datalabel)
            	{ 
    				round.itemtype = template.cat2.itemtype;
            		round.correct = 2;
    				// pick an item different from the last
    				do
    	        	    { round.catIndex = Math.floor(Math.random()*template.cat2.items.length); }
    	        	while (prevIndex1 == round.catIndex)
    	        	prevIndex1 = round.catIndex;
            	}	
            	
            	roundArray[i].push(round);
            }
        }
        
        return roundArray;
    }
    Many thanks in advance.

    J

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by jm2209 View Post
    Basically, I have managed to tweak an existing programme to meet my needs, get it hosted on a website and collecting data, but I have noticed an issue in that the categories presented at the top are not counter balanced (Online IAT). I think I have narrowed down the issue to the following piece of code (I may be wrong) and wondered if anyone could offer me any advice on what it is I need to change (Math.Random maybe?).
    What does "counter balanced" mean? Please explain your need more clearly.

    The <font> tag is long obsolete.

    The Dutch don't have any professional players, and he's one of them. - Commentator Sky Sports 2

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    What does "counter balanced" mean? Please explain your need more clearly.

    The <font> tag is long obsolete.

    The Dutch don't have any professional players, and he's one of them. - Commentator Sky Sports 2
    In short, the terms presented at the top of the screen need to swap over after each completed test (e.g., test one = Moral-left and Immoral-right, test two = Immoral-left and Moral-right etc.).

    Thanks for looking.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    do you mean you just need the words to swap every time a new question is asked? here is a simple example of that...
    Code:
    <!DOCTYPE html>
    <html lang="en-US">
    <head>
    
    <title></title>
    <style>
    #wrapper{
    margin:auto;
    width:300px;
    height:200px;
    border:solid;
    }
    #lf{
    float:left;
    }
    #rt{
    float:right;
    }
    
    </style>
    
    
    </head>
    <body>
    <div id="wrapper">
    <span id="lf">immoral</span>
    <span id="rt">moral</span>
     
    </div>
    <input type="button" value="next" id="nx"/>
    <script type="text/javascript">
    var clicks=0;
    document.getElementById("nx").onclick=function(){
    clicks++;
    document.getElementById("lf").innerHTML=clicks%2!=0?"moral":"immoral";
    document.getElementById("rt").innerHTML=clicks%2!=0?"immoral":"moral";
    }                
    </script>
    </body>
    </html>

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    or even simpler...
    Code:
    <input type="button" value="next" id="nx"/>
    <script type="text/javascript">
    
    document.getElementById("nx").onclick=function(){
    var lfI=document.getElementById("lf");
    var rtI=document.getElementById("rt");
    lfI.innerHTML=lfI.innerHTML=="immoral"?"moral":"immoral";
    rtI.innerHTML=rtI.innerHTML=="moral"?"immoral":"moral";
    }                
    </script>
    Last edited by xelawho; 04-16-2014 at 04:48 AM.

  • #6
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Phillip and Xelewho,

    Thanks for getting back to me. I have decided that this is probably one of those things i'd be better to outsource instead of trying to figure out myself.

    Thanks again for your help.

    Best.

    John


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •