overloading in jspsych? #2720
-
hello! I currently have this task I want to run that will first show participants sets of three letter strings, then six letter strings and then nine letter strings. To do this, it seems like I need to use the generateRandomStimulus function. But since javascript does not overload I cannot use a function with the same name three times. is there a work around for this in jspsych? any suggestions would be great, thanks! var exp_trial_3 = {
type: "html-keyboard-response",
stimulus: generateRandomStimulus,
choices: ['D', 'K'],
};
//indicate number of trials
var number_of_trials = 20
for (let i=0; i < number_of_trials; i++){
timeline.push(exp_trial_3);
}
function generateRandomStimulus(){
//create the alphabet and put it in an Array
var alphabet= ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
var letters= jsPsych.randomization.sampleWithReplacement(alphabet, 3);
var stim1 = letters.join('');
if (jsPsych.randomization.sampleWithReplacement([0,1],1) == 1) {
//picks random location in letter array to change
var element_location = jsPsych.randomization.sampleWithReplacement([0,1,2],1)
//Splice in a random letter from the alphabet
//the splice function changes the original variable.
//if you try to make it == something it will make a new array with inserted elements
letters.splice(element_location, 1, jsPsych.randomization.sampleWithReplacement(alphabet, 1));
var stim2 = letters.join('');
//concatenate the two words to make a DIFFERENT stimulus
var display_stim = '<p style="font-size:40px;"><b>' + stim1+' '+stim2 + '</b></p>';
console.log(stim2);
} else {
//concatenate two words to make a SAME stimulus
var display_stim = '<p style="font-size:40px;"><b>' + stim1+' '+stim1 + '</b></p>';
}
return display_stim;
}
var exp_trial_6 = {
type: "html-keyboard-response",
stimulus: generateRandomStimulus,
choices: ['D', 'K'],
};
//indicate number of trials
var number_of_trials = 20
for (let i=0; i < number_of_trials; i++){
timeline.push(exp_trial_6);
}
function generateRandomStimulus(){
//create the alphabet and put it in an Array
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
var letters = jsPsych.randomization.sampleWithReplacement(alphabet, 6);
var stim1 = letters.join('');
if (jsPsych.randomization.sampleWithReplacement([0,1],1) == 1) {
//picks random location in letter array to change
var element_location = jsPsych.randomization.sampleWithReplacement([0,1,2],1)
//Splice in a random letter from the alphabet
//the splice function changes the original variable.
//if you try to make it == something it will make a new array with inserted elements
letters.splice(element_location, 1, jsPsych.randomization.sampleWithReplacement(alphabet, 1));
var stim2 = letters.join('');
//concatenate the two words to make a DIFFERENT stimulus
var display_stim = '<p style="font-size:40px;"><b>' + stim1+' '+stim2 + '</b></p>';
console.log(stim2);
} else {
//concatenate two words to make a SAME stimulus
var display_stim = '<p style="font-size:40px;"><b>' + stim1+' '+stim1 + '</b></p>';
}
return display_stim;
}
var exp_trial_9 = {
type: "html-keyboard-response",
stimulus: generateRandomStimulus,
choices: ['D', 'K'],
};
//indicate number of trials
var number_of_trials = 20
for (let i=0; i < number_of_trials; i++){
timeline.push(exp_trial_9);
}
function generateRandomStimulus(){
//create the alphabet and put it in an Array
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
var letters = jsPsych.randomization.sampleWithReplacement(alphabet, 9);
var stim1 = letters.join('');
if (jsPsych.randomization.sampleWithReplacement([0,1],1) == 1) {
//picks random location in letter array to change
var element_location = jsPsych.randomization.sampleWithReplacement([0,1,2],1)
//Splice in a random letter from the alphabet
//the splice function changes the original variable.
//if you try to make it == something it will make a new array with inserted elements
letters.splice(element_location, 1, jsPsych.randomization.sampleWithReplacement(alphabet, 1));
var stim2 = letters.join('');
//concatenate the two words to make a DIFFERENT stimulus
var display_stim = '<p style="font-size:40px;"><b>' + stim1+' '+stim2 + '</b></p>';
console.log(stim2);
} else {
//concatenate two words to make a SAME stimulus
var display_stim = '<p style="font-size:40px;"><b>' + stim1+' '+stim1 + '</b></p>';
}
return display_stim;
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @jdistefano3 Two options that come to mind: Option 1: Rename the functions. You could just name the first Option 2: Add a parameter. You could also add a parameter to var exp_trial_6 = {
type: "html-keyboard-response",
stimulus: () => { return generateRandomStimulus(6); },
choices: ['D', 'K'],
}; |
Beta Was this translation helpful? Give feedback.
Hi @jdistefano3
Two options that come to mind:
Option 1: Rename the functions. You could just name the first
generateRandomStimulus
functiongenerateRandomStimulus3
, the secondgenerateRandomStimulus6
, and the lastgenerateRandomStimulus9
. You would need to editfunction generateRandomStimulus(){
to befunction generateRandomStimulus3(){
and then replacestimulus: generateRandomStimulus,
withstimulus: generateRandomStimulus3,
, and so on for the others.Option 2: Add a parameter. You could also add a parameter to
generateRandomStimulus
that indicated the length of the stimulus. You would edit the first line sofunction generateRandomStimulus(){
becomesfunction generateRandomStimulus(stim…