"use strict"; function drawWTP(parameters){ this.treatment=parameters.treatment this.bonusAmounts = parameters.bonusAmounts this.minBonus = parameters.minBonus this.maxBonus = parameters.maxBonus this.varname=parameters.varname this.root=parameters.root this.hidden_fields_name=parameters.hidden_fields_name this.playerID=parameters.playerID this.numForcedBoxed=parameters.numForcedBoxed this.answers = {} this.answers_list = [] var modal_div=document.createElement("div"); modal_div.innerHTML=`
` document.getElementById(this.root).appendChild(modal_div); this.drawQuestions = function(base){ for (let i = 0; i < this.bonusAmounts.length; i++) { console.log("Index:", i, "Value:", this.bonusAmounts[i]); var div = document.createElement("div"); base.appendChild(div); var p = document.createElement("h3"); p.innerHTML = "Question " + String(i + 1); div.appendChild(p); var p = document.createElement("p"); p.className = "text-center pb-2 mb-0"; if (this.treatment == "bonus") { p.innerHTML = "Do you want to open "+this.numForcedBoxed+" boxes (bonus between $"+this.minBonus + " and $"+this.maxBonus+") or do you want a bonus of $" + this.bonusAmounts[i] + " for sure?"; } else { p.innerHTML = "Do you want to open "+this.numForcedBoxed+" boxes (penalty between $"+this.minBonus + " and $"+this.maxBonus+") or do you want a penalty of $" + String(this.maxBonus + this.minBonus - this.bonusAmounts[i]) + " for sure?"; }; div.appendChild(p); var div2 = document.createElement("div"); div2.className = "d-flex justify-content-around"; div.appendChild(div2); var taskButton=document.createElement("button") taskButton.setAttribute("type","button"); taskButton.setAttribute("id","taskButton_id_"+String(i)); taskButton.className="btn btn-sm btn-secondary"; if (this.treatment == "bonus") { taskButton.innerHTML="Open "+this.numForcedBoxed + " boxes and look for best bonus"; } else { taskButton.innerHTML="Open "+this.numForcedBoxed + " boxes and look for best penalty"; } taskButton.style.width = "300px"; taskButton.addEventListener('click', () => { document.getElementById("taskButton_id_"+String(i)).classList.remove("btn-success"); document.getElementById("sureBonusButton_id_"+String(i)).classList.remove("btn-success"); document.getElementById("taskButton_id_"+String(i)).classList.add("btn-success"); this.answers[this.bonusAmounts[i]] = "task"; if (Object.keys(this.answers).length == this.bonusAmounts.length) { document.getElementById("next_button_id_de").style.display = ""; } }); div2.appendChild(taskButton); var sureBonusButton=document.createElement("button") sureBonusButton.setAttribute("type","button"); sureBonusButton.setAttribute("id","sureBonusButton_id_"+String(i)); sureBonusButton.className="btn btn-sm btn-secondary"; if (this.treatment == "bonus") { sureBonusButton.innerHTML="Get $"+this.bonusAmounts[i] + " bonus for sure"; } else { sureBonusButton.innerHTML="Get $"+String(this.maxBonus + this.minBonus - this.bonusAmounts[i]) + " penalty for sure"; } sureBonusButton.style.width = "300px"; sureBonusButton.addEventListener('click', () => { document.getElementById("taskButton_id_"+String(i)).classList.remove("btn-success"); document.getElementById("sureBonusButton_id_"+String(i)).classList.remove("btn-success"); document.getElementById("sureBonusButton_id_"+String(i)).classList.add("btn-success"); this.answers[this.bonusAmounts[i]] = "sure"; if (Object.keys(this.answers).length == this.bonusAmounts.length) { document.getElementById("next_button_id_de").style.display = ""; } }); div2.appendChild(sureBonusButton); var hr = document.createElement("hr"); div.appendChild(hr); }; document.getElementById('next_button_id_de').addEventListener('click', () => { console.log("helloooo") document.getElementById("confirm_modal_text_id").innerHTML = "Are you sure about your answers?
"; this.answers_list.push(this.answers) document.getElementById(this.varname).value=JSON.stringify(this.answers_list); let highestTaskKey = -Infinity; let lowestSureKey = Infinity; for (const key in this.answers) { if (this.answers[key] === "task" && Number(key) > highestTaskKey) { highestTaskKey = Number(key); } if (this.answers[key] === "sure" && Number(key) < lowestSureKey) { lowestSureKey = Number(key); } }; if (highestTaskKey !== -Infinity && lowestSureKey !== Infinity && highestTaskKey > lowestSureKey) { console.log("Highest key with 'task' value:", highestTaskKey); console.log("Lowest key with 'sure' value:", lowestSureKey); var text; if (this.treatment == "bonus") { text = 'We noticed that you chose "Open '+this.numForcedBoxed+' boxes and look for best bonus" over "Get $'+highestTaskKey+' bonus for sure." However, when the sure bonus was only $'+lowestSureKey+', you chose "Get $'+lowestSureKey+' bonus for sure" over "Open '+this.numForcedBoxed+' boxes and look for best bonus."