Skip to content

Instantly share code, notes, and snippets.

@hrehman200
Last active April 15, 2024 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrehman200/21f2611fc3e9dc6dc11ffd83b40e7c0c to your computer and use it in GitHub Desktop.
Save hrehman200/21f2611fc3e9dc6dc11ffd83b40e7c0c to your computer and use it in GitHub Desktop.
Qualtrics Game Gist
Qualtrics.SurveyEngine.addOnload(function()
{
Qualtrics.SurveyEngine.setEmbeddedData('money', 10)
});
Qualtrics.SurveyEngine.addOnReady(function()
{
function generateRandomNumber(boxDiv, mean, sd) {
const randomNumber = generateRandomNormal(mean, sd);
const roundedNumber = Math.round(randomNumber * 100) / 100;
jQuery(boxDiv).find('div').html(roundedNumber);
return roundedNumber;
}
function generateRandomNormal(mean, sd) {
let u1, u2;
let z0;
do {
u1 = Math.random();
u2 = Math.random();
z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
} while (u1 <= 1e-6);
// Apply mean and standard deviation
const randomNormal = z0 * sd + mean;
return randomNormal;
}
// -------------------------------------------------------------
jQuery('.box div').css('visibility', 'hidden');
let bulbDiv = jQuery('div[data-color="bulb"]')
bulbDiv.css('visibility', 'hidden');
// fill this array with clicks on which you want the bulb to appear
let bulbToAppearOnClicks = [2, 5, 7, 10];
let clicks = 0;
let totalClicksAllowed = 25;
let money = parseFloat(jQuery('#money').html());
let moneyToDeductOnBulbClick = 0.25;
let bulbTips = [
'This is tip 1',
'This is tip 2',
'This is tip 3',
'This is tip 4',
'This is tip 5',
'This is tip 6',
'This is tip 7',
'This is tip 8',
'This is tip 9',
'This is tip 10',
'This is tip 11',
'This is tip 12',
'This is tip 13',
'This is tip 14',
'This is tip 15',
'This is tip 16',
'This is tip 17',
'This is tip 18',
'This is tip 19',
'This is tip 20',
];
jQuery('.box').on('click', function(e) {
if(clicks >= totalClicksAllowed) {
alert("You have reached your click limit. Please proceed to place your bet.");
return;
}
let box = jQuery(e.target).parent()
let color = box.data('color')
if(color == undefined)
return;
let mean = 5;
let stdDev = Math.sqrt(3);
jQuery('.box div').css('visibility', 'hidden');
jQuery(box).find('div').css('visibility', 'visible');
/*
Box A distribution: mean = 1.3, SD = .6
Box B distribution: mean = 1.2, SD = .5
Box C distribution: mean = 1, SD =.5
*/
switch(color) {
case 'green':
mean = 1.3
stdDev = 0.6
clicks++;
break;
case 'red':
mean = 1.2
stdDev = 0.5
clicks++;
break;
case 'yellow':
mean = 1
stdDev = 0.5
clicks++;
break;
case 'bulb':
// clicking bulb also counts as a click
clicks++;
break;
}
if(color != 'bulb') {
let generatedNo = generateRandomNumber(box, mean, stdDev)
jQuery(box).find('div').html(generatedNo)
} else {
alert(bulbTips[0]);
bulbTips = bulbTips.slice(1);
money -= moneyToDeductOnBulbClick;
jQuery('#money').html(parseFloat(money).toFixed(2))
}
Qualtrics.SurveyEngine.setEmbeddedData('money', parseFloat(money).toFixed(2))
if(bulbToAppearOnClicks.indexOf(clicks) != -1) {
bulbDiv.css('visibility', 'visible');
} else {
bulbDiv.css('visibility', 'hidden');
}
if(clicks >= totalClicksAllowed) {
alert("You have reached your click limit. Please proceed to place your bet.");
}
})
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment