Javascript | XM Community
Skip to main content
In my survey, participants have to bid on shares of both ethical and non-ethical companies with 2 different wallets they have been given. A bid price and random price determine whether the share is purchased. This changes the wallet. Using JavaScript, I want to show what their bid has done to their wallet. Unfortunately the codes i am using do not work. Can someone help me please?

these are my codes:

Qualtrics.SurveyEngine.addOnReady(function()
{
    /*Place your JavaScript here to run when the page is fully displayed*/
    
    // Initialize wallet balances
var ownWallet = 10;
var charityWallet = 5;

// Generate a random price between 0 and 10
var randomPrice = (Math.random() * 10).toFixed(2); // Ensure the random price has two decimal places

// Calculate the profit (either 5 or 2.5)
var profit = Math.random() < 0.5 ? 5 : 2.5;

// Get the participant's bid price from your Qualtrics question
var participantBid = Number("${e://Field/Participants bid price}");

// Check if the participant's bid is higher than the random price
if (participantBid >= randomPrice) {
    // Update the own wallet balance
    ownWallet = ownWallet - randomPrice + profit;

    // Determine the amount to be subtracted from the charity wallet (either 2.5 or 0.5)
    var charitySubtraction = Math.random() < 0.5 ? 2.5 : 0.5;

    // Update the charity wallet balance
    charityWallet = charityWallet - charitySubtraction;

    // Inform the participant that they bought the share
    var FL_2 = document.getElementById('share_status_element'); // Replace with the actual element ID
    if (FL_2) {
        shareStatusElement.innerHTML = 'You bought the share at the random price of ' + randomPrice + ' euros.';
    }
} else {
    // Inform the participant that they did not buy the share
    var FL_2 = document.getElementById('share_status_element'); // Replace with the actual element ID
    if (FL_2) {
        shareStatusElement.innerHTML = 'You did not buy the share at the random price of ' + randomPrice + ' euros.';
    }
}

});
 

Hi @Otte Wienese 
Unfortunatelly, no one can help without knowing your setup.

And sorry if i’m being wrong but I believe this is a chatGPT generated code with a lot of comment like: “// Replace with the actual element ID”. It just giving you the idea, not something you can copy&paste and execute. The code looks decent but it’s not the Qualtrics way
You should breakdown your setup, which question you’re using to get the bid, wallet amount, charity randomness, where do you want to show the result….
 


Leave a Reply