Last active
May 27, 2024 15:24
-
-
Save hrehman200/21f2611fc3e9dc6dc11ffd83b40e7c0c to your computer and use it in GitHub Desktop.
Qualtrics Game Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
if(boxDiv != null) | |
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 Math.abs(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 bulbClickedOnce = false; | |
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', | |
]; | |
let means = { | |
green: 1.3, | |
red: 1.2, | |
yellow: 1, | |
}; | |
let stdDevs = { | |
green: 0.6, | |
red: 0.5, | |
yellow: 0.5, | |
}; | |
let random25NoPerBox = { | |
green: [], | |
red: [], | |
yellow: [], | |
}; | |
let colors = Object.keys(means); | |
for (let h=0; h<colors.length; h++) { | |
let color = colors[h]; | |
for (let i=0; i<25; i++) { | |
let randomNo = generateRandomNumber(null, means[color], stdDevs[color]); | |
random25NoPerBox[color].push(randomNo); | |
} | |
} | |
random25NoPerBox.green = random25NoPerBox.green.sort((a, b) => { return a - b; }) | |
random25NoPerBox.red = random25NoPerBox.red.sort((a, b) => { return a - b; }) | |
random25NoPerBox.yellow = random25NoPerBox.yellow.sort((a, b) => { return a - b; }) | |
Qualtrics.SurveyEngine.setEmbeddedData('random25NoPerBox', random25NoPerBox) | |
console.log(random25NoPerBox) | |
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++; | |
bulbClickedOnce = true; | |
break; | |
} | |
if(color != 'bulb') { | |
let generatedNo = random25NoPerBox[color][clicks]; //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 && !bulbClickedOnce) { | |
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