我正在用Qualtrics和JavaScript建立一个关于投票 Select 和违反民主规范的联合实验.在典型的联合操作中,您让所有属性级别都是随机的.然而,这在选举中是不现实的,因 for each 政党只有一名候选人.

以下是我现在使用的代码:

    // Set number of choices (number of elections);
    var CJ_numChoice = 3;

    // Vectors containing all attribute levels:
    var CJ_eth = ["White", "South Asian", "East Asian", "Indigenous"];
    var CJ_gender = ["Man", "Woman"];
    var CJ_age = ["35", "65"];
    var CJ_pid = ["Liberal", "Conservative", "NDP"];
    var CJ_occup = ["Lawyer", "Political Staffer", "Construction worker", "Business person"];
    var CJ_exp = ["No experience", "Mayor", "Member of provincial legislature", "Member of Parliament"];
    var CJ_wel = ["Spending on welfare should be kept the same", "Spending on welfare should be increased", "Spending on welfare should be reduced"];
    var CJ_enviro = ["Must find a balance between reducing greenhouse gas emissions and maintaining jobs in the oil and gas sector", "Must prioritize reducing greenhouse gas emissions over the jobs in the oil and gas sector", "Must prioritize jobs in the oil and gas sector over reducing greenhouse gas emissions"];
    var CJ_court = ["A prime minister should always adhere to court decisions even though they might seem politicized", "A prime minister should not be bound by court decisions he or she regards as politicized"];
    var CJ_prot = ["The prime minister should not be allowed to ban non-violent opposition protests under any circumstances", "The prime minister should be free to ban non-violent opposition protests if they are disruptive to society"];

    // Fisher - Yates shuffle:
    function shuffle(array) {
        for (var i = array.length - 1; i > 0; i--) {
            var j = Math.floor(Math.random() * (i + 1));
            var temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
        return array;
    }

    // Shuffle a vector, choose the first entry:
    function shuffle_one(theArray) {
        var out = shuffle(theArray);
        return out[0];
    }

    var CJ_attribnames = ["Ethnicity", "Gender", "Age", "Party", "Previous occupation", "Prior political experience", "Position on welfare spending", "Position on environmental issues", "Position on courts", "Position on press freedom", "Position on protests rights"];

    function randomize(i) {
        // Randomly draw the attributes for each candidate
        var c1 = [shuffle_one(CJ_eth), shuffle_one(CJ_gender), shuffle_one(CJ_age), shuffle_one(CJ_pid), shuffle_one(CJ_occup), shuffle_one(CJ_exp), shuffle_one(CJ_wel), shuffle_one(CJ_enviro), shuffle_one(CJ_court), shuffle_one(CJ_prot)];
        var c2 = [shuffle_one(CJ_eth), shuffle_one(CJ_gender), shuffle_one(CJ_age), shuffle_one(CJ_pid), shuffle_one(CJ_occup), shuffle_one(CJ_exp), shuffle_one(CJ_wel), shuffle_one(CJ_enviro), shuffle_one(CJ_court), shuffle_one(CJ_prot)];

        
        
        // Save the results
        for (var k = 1; k <= CJ_attribnames.length; k++) {
            var CJ_index = k - 1;
            Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_name', CJ_attribnames[CJ_index]);
            Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_c1', c1[CJ_index]);
            Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_c2', c2[CJ_index]);
        }
    }

    for (var i = 1; i <= CJ_numChoice; i++) {
        randomize(i);
    }
});

你能帮我弄清楚如何确保两个候选人的政党在以下方面总是不同的风格:让候选人S的政党是随机的.如果候选人1是自由党或新民主党候选人2只能是保守党.如果候选人1是保守党,候选人2可以随机成为自由党或新民主党.

推荐答案

您必须对代码进行一些修改.最简单的解决方案是在randomize()函数的无限循环中添加一条if语句,判断各方是否不同,如果不同,则退出循环.

function randomize(i) {
    var c1 = [shuffle_one(CJ_eth), shuffle_one(CJ_gender), shuffle_one(CJ_age), shuffle_one(CJ_pid), shuffle_one(CJ_occup), shuffle_one(CJ_exp), shuffle_one(CJ_wel), shuffle_one(CJ_enviro), shuffle_one(CJ_court), shuffle_one(CJ_prot)];


    var c2 = [shuffle_one(CJ_eth), shuffle_one(CJ_gender), shuffle_one(CJ_age), shuffle_one(CJ_pid), shuffle_one(CJ_occup), shuffle_one(CJ_exp), shuffle_one(CJ_wel), shuffle_one(CJ_enviro), shuffle_one(CJ_court), shuffle_one(CJ_prot)];



    while (true) {
        if (c2[3] !== c1[3]) {
            break;
        }
        
        c2[3] = shuffle_one(CJ_pid);
    } 

    // Save the results
    for (var k = 1; k <= CJ_attribnames.length; k++) {
        var CJ_index = k - 1;
        Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_name', CJ_attribnames[CJ_index]);
        Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_c1', c1[CJ_index]);
        Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_c2', c2[CJ_index]);
    }
}

以下是对代码所做的修改:

  1. 我已经添加了一个无限次运行的While循环
  2. 判断c2的参与方是否与c1的参与方相同
  3. 如果各方不同,就会跳出循环
  4. 如果双方是相同的,随机地,将 Select 另一方,循环将重复.

上面的解决方案是随机 Select 双方.


编辑

如果要根据c1所属的参与方为c2分配参与方,可以执行以下操作

function randomize(i) {
    var c1 = [shuffle_one(CJ_eth), shuffle_one(CJ_gender), shuffle_one(CJ_age), shuffle_one(CJ_pid), shuffle_one(CJ_occup), shuffle_one(CJ_exp), shuffle_one(CJ_wel), shuffle_one(CJ_enviro), shuffle_one(CJ_court), shuffle_one(CJ_prot)];


    var c2 = [shuffle_one(CJ_eth), shuffle_one(CJ_gender), shuffle_one(CJ_age), '', shuffle_one(CJ_occup), shuffle_one(CJ_exp), shuffle_one(CJ_wel), shuffle_one(CJ_enviro), shuffle_one(CJ_court), shuffle_one(CJ_prot)];


    // Ensure a different party for candidate 2 based on partyC1
    if (c1[3] === 'Liberal' || c1[3] === 'NDP') {
        c2[3] = 'Conservative';
    } else if (partyC1 === 'Conservative') {
        c2[3] = Math.random() < 0.5 ? 'Liberal' : 'NDP';
    }


    // Save the results
    for (var k = 1; k <= CJ_attribnames.length; k++) {
        var CJ_index = k - 1;
        Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_name', CJ_attribnames[CJ_index]);
        Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_c1', c1[CJ_index]);
        Qualtrics.SurveyEngine.setEmbeddedData('c' + i + '_attrib' + k + '_c2', c2[CJ_index]);
    }
}

已完成的修改:

  1. 像往常一样创建c1,使用空方创建c2
  2. 如果是c1,则使用if语句判断参与方
  3. 如果c1属于自由党或NDP,则将c2指定为保守党.如果c1是保守的,随机 Select 自由党或新民主党

Javascript相关问答推荐

在贝塞尔曲线的直线上找不到交叉点:(使用@Pomax的bezier.js)

我在这个黑暗模式按钮上做错了什么?

如何避免移动设备中出现虚假调整大小事件?

每次子路由重定向都会调用父加载器函数

Chart.js V4切换图表中的每个条,同时每个条下有不同的标签.怎么做?

在JavaScript中声明自定义内置元素不起作用

使用JavaScript重新排序行

将2D数组转换为图形

如何在不创建新键的情况下动态更改 map 中的项目?

配置WebAssembly/Emscripten本地生成问题

我们如何从一个行动中分派行动

简单的PayPal按钮集成导致404错误

Prisma具有至少一个值的多对多关系

如何在我的Next.js项目中.blob()我的图像文件?

Reaction-SWR-无更新组件

将基元传递给THEN处理程序

用另一个带有类名的div包装元素

如何在独立的Angular 应用程序中添加Lucide-Angel?

使用props 将VUE 3组件导入JS文件

调试jQuery代码以获取所有行的总和(票证类型)