Millisecond Forums

Generate Participant ID from JavaScript

https://forums.millisecond.com/Topic28539.aspx

By IR1999 - 3/18/2020

Hi,
I want to generate a participant ID from a custom JavaScript code (see below) but when I my inquisit study on millisecond, the number comes up as a secondary notification and doesn't let me start the study. I am confused as to out to input a number I output from javascript back into inquisit. Or is this not possible?

function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;

// While there remain elements to shuffle...
while (0 !== currentIndex) {

// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;

// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}

return array;
}

// Used like so
var arr = [2, 11, 37, 42];
arr = shuffle(arr);
console.log(arr);
alert(arr[0])


By Dave - 3/18/2020

IR1999 - 3/18/2020
Hi,
I want to generate a participant ID from a custom JavaScript code (see below) but when I my inquisit study on millisecond, the number comes up as a secondary notification and doesn't let me start the study. I am confused as to out to input a number I output from javascript back into inquisit. Or is this not possible?

function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;

// While there remain elements to shuffle...
while (0 !== currentIndex) {

// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;

// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}

return array;
}

// Used like so
var arr = [2, 11, 37, 42];
arr = shuffle(arr);
console.log(arr);
alert(arr[0])



To work properly, your code needs to return the result, not pop an alert.

function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;

// While there remain elements to shuffle...
while (0 !== currentIndex) {

// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;

// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}

return array;
}

// Used like so
var arr = [2, 11, 37, 42];
arr = shuffle(arr);
return(arr[0])