Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7672589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:19:15+00:00 2026-05-31T16:19:15+00:00

I am a novice programmer and apologize upfront for the complicated question. I am

  • 0

I am a novice programmer and apologize upfront for the complicated question.

I am trying to create a lexical decision task for experimental research, in which respondents must decide if a series of letters presented on the screen make a “word” or “not a word”. Everything works reasonably well except for the bit where I want to randomly select a word (category A) or nonword (category B) for each of 80 trials from a separate input file (input.txt). The randomization works, but some elements from each list (category A or B) are skipped because I have used “round.catIndex = j;” where “j” is a loop for each successive trial. Because some trials randomly select from Category A and other from Category B, “j” does not move successively down the list for each category. Instead, elements from the Category A list may be selected from something like 1, 2, 5, 8, 9, 10, and so on (it varies each time because of the randomization).

To make a long story short(!), how do I create a counter that will work within the for-loop for each trial, so that every word and nonword from Category A and B, respectively, will be used for the lexical decision task? Everything I have tried thus far does not work properly or breaks the javascript entirely.

Below is my code snippet and the full code is available at http://50.17.194.59/LDT/trunk/LDT.js. Also, the full lexical decision task can be accessed at http://50.17.194.59/LDT/trunk/LDT.php. Thanks!

    function initRounds()
    {
        numlst = [];
        for (var k = 0; k<numrounds; k++)
            {
                if (k % 2 == 0) numlst[k] = 0;
                else numlst[k] = 1;
            }
        numlst.sort(function() {return 0.5 - Math.random()})

        for (var j = 0; j<numrounds; j++)
            {       
                var round = new LDTround();
                if (numlst[j] == 0)
                    {
                        round.category = input.catA.datalabel;
                    }
                else if (numlst[j] == 1)
                    {
                        round.category = input.catB.datalabel;
                    }

                // pick a category & stimulus
                    if (round.category == input.catA.datalabel) 
                        {
                            round.itemtype = input.catA.itemtype;
                            round.correct = 1;
                            round.catIndex = j;
                        }
                    else if (round.category == input.catB.datalabel)
                        { 
                            round.itemtype = input.catB.itemtype;
                            round.correct = 2;
                            round.catIndex = j;   
                        }       
                    roundArray[i].push(round);
                }
        return roundArray;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:19:17+00:00Added an answer on May 31, 2026 at 4:19 pm

    You can use the comma operator to declare multiple variables and execute multiple statements within a single for loop.

    In your case, you could do something like:

    for(var CatAIndex = 0, CatBIndex = 0; CatAIndex+CatBIndex < numrounds; incrementA ? CatAIndex++ : CatBIndex++) {
        // Insert your code here
    }
    

    I chose those verbose variable names to make it more clear. You’d have two separate indices for category A and B, and you compare the sum of the two versus the number of rounds you want to run. Then inside of your for loop somewhere, you set the boolean incrementA to either true or false to indicate which one to increment.

    That roughly matches what you’re asking for, but I think what you’d prefer is to use a combination of Math.random, <array>.splice and <array>.length to get a random word/nonword from each list, rather than producing a predictable order for selection. Then you don’t even care what the indices are for the two categories and you can go back to a simple for(var i = 0; i < numrounds; i++) type of loop.

    If the latter is what you really want, leave a comment on this answer and I’ll update it with another example.

    EDIT:

    Okay, I’m assuming that the actual number and order of words and non-words is not really defined by your test, because otherwise a user could pick up the word/non-word pattern and Christmas Tree the test. I’m also assuming that you have two array of words and non-words called catA and catB in the global scope. Below is a function that will do the following:

    1. Randomly pick a word or non-word.
    2. Never repeat a word or non-word pick (meaning that technically it becomes more deterministic the closer to the end of the list you are.
    3. Until all words are exhausted, at which point it will automatically “refresh” its list from the catA and catB arrays. (So you can set numrounds to +inf if you like.)

    .

    var pickAWord = (function outerScope() {
        var allWords = [];
        return function innerClosure() {
            if(allWords.length == 0) {
                allWords = [].concat(catA, catB);
            }
            return allWords.splice(Math.floor(Math.random()*allWords.length), 1)[0];
        };
    })();
    

    The function is using the functional programming concept of closures to create a persisted “global-like” variable, allWords that only it can see. The function automatically refreshes the array with all of the words when the length of the array reaches zero (like it is from the start) using the globals catA and catB. To use it in a for loop, simply:

    for(var i = 0; i < numrounds; i++) {
        var wordToUse = pickAWord();
        // Do something
    }
    

    If you need to guarantee that an equal number of catA and catB words are used, the outerScope function will need to keep track of three variables: copies of catA and catB, and an array the same size as numrounds, half of which are true and half false. splice randomly from this true/false array, and then splice randomly from either catA or catB depending on whether it’s true or false. Then you function will need code to “refresh” all of these closure variables, but it would be essentially the same as how the function is written above.

    Sorry if the function is a bit complex, but you see how easy it is to use, right? 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a novice Javascript programmer, so I apologize if this question makes no sense.
I am a novice programmer who is trying to teach myself to code, specifically
I am a novice programmer at best, but I am trying to play a
Novice programmer asking first question on stack-overflow. I am writing an app for mac
Novice programmer here. trying to learn static methods and recursion. No idea why I
This is not homework. I am a beginner (novice) java programmer, trying to read
I'll admit I'm a novice programmer and really the only experience I have is
I am a novice-intermediate programmer taking a stab at AJAX. While reading up on
I'm a very novice OCaml programmer so please forgive me if this is a
I'm a novice-to-intermediate JavaScript/jQuery programmer, so concrete/executable examples would be very much appreciated. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.