jsPsych randomization function repeating final trial in loop

325 Views Asked by At

I'm using a for loop to create test trials in which each test item is presented once but the final item is always presented twice.The loop is meant to show each rocksize once in a random order. I've tried various randomisation functions (shuffle, shuffleNoRepeats, sampleWithoutReplacement, repeat) but none of them does the job. I inherited this code and am adapting it rather than writing the whole thing myself. I also noticed the data arguments don't record the actual sizes of the items presented but records them in ascending order.

Perhaps there's an issue with the loop but I haven't been able to identify what the problem is, so would be really grateful if someone could have a look and see whether they can spot what's wrong.

Here is my code:

var rocksize = [24, 34, 43, 53, 62, 71, 90];

var rateqorder = jsPsych.randomization.repeat([0, 1, 2, 3, 4, 5, 6], 1);
// var rateqorder = jsPsych.randomization.shuffleNoRepeats([0, 1, 2, 3, 4, 5, 6]); // A few different things I've tried
    console.log(rocksize)
    console.log(rateqorder)


    for (var i = 0; i < rateqorder.length; i++) {
        var rate = {
            type: "html-button-response",
            timing_post_trial: 500,
            stimulus:
                '<div class="ratecontainer">' +
                '<div class="head"></div>' +
                '<div class="head"></div>' +
                '<div class="head"></div>' +
                '<div class="mid"></div>' +
                '<div class="center">' +
                '<div class="raterock">' +
                '<span class="ratedot" style="height:' + rocksize[rateqorder[i]] + 'px; width:' + rocksize[rateqorder[i]] + 'px;"></span>' +
                '</div></div>' +
                '<div class="mid"></div>' +
                '<div class="foot"></div>' +
                '<div class="foot"></div>' +
                '<div class="foot"></div>' +
                '</div>' +
                '<span><p>Based on what you have learned so far, how likely is it that a Sodor sphere of this size has plaxium coating?<br><br><br></p></span>',
            choices: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
            prompt: "<span style='font-size:15px'>[Very Unlikely]" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
                "[Very Likely]</span>",
            data: {
                trial: rateqorder[i],
                rocksize: rocksize[i] 
            }
        }
        timeline.push(rate)


        var wait = {
            type: "html-keyboard-response",
            stimulus: " ",
            choices: jsPsych.NO_KEYS,
            trial_duration: 1000,
        }
        timeline.push(wait)

    }
    timeline.push(rate) 

How can I stop it repeating the final trial?

1

There are 1 best solutions below

0
On

Remove the last timeline.push(rate) to get rid of the last trial showing twice.

Also, here's a slightly more robust version of your code with minimal changes. Notice how I only shuffle the rocksize's array before creating trials with them (with number of trials N_TRIALS equal to the rocksize's array length)

<!DOCTYPE html>
<html>
  <head>
    <title>My experiment</title>
    <script src="jspsych.js"></script>
    <script src="jspsych-html-button-response.js"></script>
    <script src="jspsych-html-keyboard-response.js"></script>
    <link href="jspsych.css" rel="stylesheet" type="text/css" />
</head>
<body></body>
<script>

var timeline = [];

var rocksize = jsPsych.randomization.shuffle([24, 34, 43, 53, 62, 71, 90]);
const N_TRIALS = rocksize.length

for (var trial_ind = 0; trial_ind < N_TRIALS; trial_ind++) {
    var rate = {
        type: "html-button-response",
        timing_post_trial: 500,
        stimulus: 'rock size: ' + rocksize[trial_ind],
        choices: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"],
        prompt: "MY PROMPT",
        data: {
            trial: trial_ind,
            rocksize: rocksize[trial_ind] 
        }
    }
    timeline.push(rate)


    var wait = {
        type: "html-keyboard-response",
        stimulus: " ",
        choices: jsPsych.NO_KEYS,
        trial_duration: 1000,
    }
    timeline.push(wait)

}
// timeline.push(rate) -- remove this to get yours to work 

jsPsych.init({timeline: timeline})

    </script>
</html>

Note that I have used jsPsych v6.3.1 as it seems you have used version before 7.0.