Hello everyone, I am using the code below. However, this code prevents displaying the embedded data named current assets. I could not understand why this problem occurs. Can anyone help me with this?
Thank you in advance.
Qualtrics.SurveyEngine.addOnReady(function () {
// Load current assets from embedded data
let currentAssets = Qualtrics.SurveyEngine.getEmbeddedData("Current_Assets") || "";
currentAssets = currentAssets.length > 0 ? currentAssets.split(", ") : [];
// Define asset details with prices and descriptions
const assetDetails = {
"Buy_Cheap_Car": { price: 500, description: "A Cheap Car" },
"Buy_Normal_Car": { price: 1000, description: "A Normal Car" },
"Buy_Expensive_Car": { price: 1500, description: "An Expensive Car" },
"Buy_Cheap_House": { price: 1000, description: "A Cheap House" },
"Buy_Normal_House": { price: 1500, description: "A Normal House" },
"Buy_Expensive_House": { price: 2000, description: "An Expensive House" },
"Invest_In_Business": { price: 2500, description: "Extended Your Business" },
"Invest_In_Stocks": { price: 500, description: "Stocks" },
"Do_Not_Buy": { price: 0, description: "It's Empty" }
};
// Function to calculate profit or loss from selling an asset
function calculateProfitOrLoss(price) {
const isProfit = Math.random() < 0.5; // 50% chance of profit or loss
return isProfit ? price * 1.4 : price * 0.8; // 40% profit or 20% loss
}
// Update the display of current assets
function updateCurrentAssets() {
const display = document.querySelector("#currentAssets");
if (display) {
display.textContent = "Current Assets: " + (currentAssets.length > 0 ? currentAssets.join(", ") : "None");
}
// Disable checkboxes for assets not owned
document.querySelectorAll("input[type='checkbox']").forEach(choice => {
const assetId = choice.id;
const asset = assetDetails[assetId] || { description: "Unknown" };
if (!currentAssets.includes(asset.description)) {
choice.disabled = true; // Disable if the asset is not owned
} else {
choice.disabled = false; // Enable if the asset is owned
}
});
}
// Update the total selling revenue based on selected assets
function updateSellingRevenue() {
let sellingRevenue = 0;
let selectedItems = [];
// Calculate revenue for each selected checkbox
document.querySelectorAll("input[type='checkbox']:checked").forEach(choice => {
const assetId = choice.id;
const asset = assetDetails[assetId] || { price: 0, description: "Unknown" };
if (currentAssets.includes(asset.description)) {
const saleRevenue = calculateProfitOrLoss(asset.price);
sellingRevenue += saleRevenue;
selectedItems.push(asset.description);
} else {
choice.checked = false; // Uncheck if the asset is not in current assets
}
});
// Update the selling revenue display
const sellingRevenueDisplay = document.querySelector("#sellingRevenueDisplay");
if (sellingRevenueDisplay) {
sellingRevenueDisplay.textContent = "Selling Revenue: " + sellingRevenue.toFixed(2) + " ECU";
}
// Store the selling revenue as embedded data
Qualtrics.SurveyEngine.setEmbeddedData("Selling_Revenue", sellingRevenue.toFixed(2));
}
// Add event listeners to checkboxes to update current assets and revenue
document.querySelectorAll("input[type='checkbox']").forEach(choice => {
choice.addEventListener("change", function () {
const assetId = choice.id;
const asset = assetDetails[assetId] || { description: "Unknown" };
// Update current assets array based on checkbox selection
if (choice.checked) {
if (!currentAssets.includes(asset.description)) {
currentAssets.push(asset.description); // Add to current assets if checked
}
} else {
currentAssets = currentAssets.filter(item => item !== asset.description); // Remove from current assets if unchecked
}
// Update embedded data for current assets and recalculate revenue
Qualtrics.SurveyEngine.setEmbeddedData("Current_Assets", currentAssets.join(", "));
updateCurrentAssets();
updateSellingRevenue();
});
});
// Finalize the sale when the button is clicked
const finalizeSaleButton = document.querySelector("#finalizeSaleButton");
if (finalizeSaleButton) {
finalizeSaleButton.addEventListener("click", function () {
// You could add further actions here, like saving data or moving to the next page
alert("Sale Finalized! Total Revenue: " + document.querySelector("#sellingRevenueDisplay").textContent);
});
}
// Initialize the display and revenue calculations on page load
updateCurrentAssets();
updateSellingRevenue();
});