首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果参与者提供了n个正确答案,则跳过时间线的其余部分(jspsych)

如果参与者提供了n个正确答案,则跳过时间线的其余部分(jspsych)
EN

Stack Overflow用户
提问于 2020-02-14 18:06:47
回答 1查看 277关注 0票数 1

我在一条时间线上有n次决策试验。正确答案被记录为数据属性。如果参与者提供了固定数量的正确答案,我想跳过剩下的时间线。这需要一个条件,但我的不起作用。

代码语言:javascript
复制
var if_node = {
    timeline: [test_procedure],
    conditional_function: function(){
          var data = jsPsych.data.get()
          var correct_count = data.filter({correct: true}).count();
          return correct_count < 2
          }
    }

下面是可重现的代码(需要jspsych-6)。

代码语言:javascript
复制
<!DOCTYPE html>
<html>
  <head>
    <title>My experiment</title>
    <script src="jspsych-6/jspsych.js"></script>
    <script src="jspsych-6/plugins/jspsych-html-keyboard-response.js"></script>
    <script src="jspsych-6/plugins/jspsych-html-button-response.js"></script>
    <link href="jspsych-6/css/jspsych.css" rel="stylesheet" type="text/css"></link>
  </head>
  <body></body>
  <script>

    /* create timeline */
    var timeline = [];

    /* test trials */

    var test_stimuli = [
      { word: "table", data: { false_word: '0' } },
      { word: "tfble", data: { false_word: '1' } },
      { word: "tablw", data: { false_word: '1' } }
    ];

    var trial = {
        type: ["html-button-response"],
        stimulus: jsPsych.timelineVariable('word'),
        choices: ["real", "fake"],
        margin_vertical: "0px",
        margin_horizontal: "8px",
        response_ends_trial: true,
        post_trial_gap: [500],
        data: jsPsych.timelineVariable('data'),
        on_finish: function(data){
          data.correct = data.false_word == data.button_pressed
        }
    };

    var test_procedure = {
      timeline: [trial],
      timeline_variables: test_stimuli
    }
    //timeline.push(test_procedure);

    var if_node = {
    timeline: [test_procedure],
    conditional_function: function(){
          var data = jsPsych.data.get()
          var correct_count = data.filter({correct: true}).count();
          return correct_count < 2
          }
    }

    timeline.push(if_node);

    /* start the experiment */
    jsPsych.init({
      timeline: timeline,
      on_finish: function() {
        jsPsych.data.displayData();
      }
    });
  </script>
</html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-30 05:07:18

您可以在实验开始时声明一个correct_count变量,并在试验正确时将该变量加1,然后在if_node中检查该计数器是否大于一个数字,并相应地执行时间线。

我对您的代码做了一些更改,并用注释突出显示了我的更改。请看下面的内容。

代码语言:javascript
复制
<!DOCTYPE html>
<html>
  <head>
    <title>My experiment</title>
    <script src="jspsych-6/jspsych.js"></script>
    <script src="jspsych-6/plugins/jspsych-html-keyboard-response.js"></script>
    <script src="jspsych-6/plugins/jspsych-html-button-response.js"></script>
    <link href="jspsych-6/css/jspsych.css" rel="stylesheet" type="text/css"></link>
 
  </head>
  <body></body>
  <script>

    /* create timeline */
    var timeline = [];

    /* here I create correct_count variable to count correct trials */ 
    var correct_count = 0;
    /* test trials */

    var test_stimuli = [
      { word: "table", data: { false_word: '0' } },
      { word: "tfble", data: { false_word: '1' } },
      { word: "tablw", data: { false_word: '1' } }
    ];

    var trial = {
        type: ["html-button-response"],
        stimulus: jsPsych.timelineVariable('word'),
        choices: ["real", "fake"],
        margin_vertical: "0px",
        margin_horizontal: "8px",
        response_ends_trial: true,
        post_trial_gap: [500],
        data: jsPsych.timelineVariable('data'),
        on_finish: function(data){
          data.correct = data.false_word == data.button_pressed
          console.log(data.correct )
          if (data.correct = true){ // here I increment the counter variable if the response is correct
            correct_count++
            console.log(correct_count)
          } 
          
        }
    };

    var test_procedure = {
      timeline: [trial],
      timeline_variables: test_stimuli
    }
    //timeline.push(test_procedure);

    var if_node = {
    timeline: [test_procedure],
    conditional_function: function(){
      console.log(correct_count)
      if (correct_count < 2) { //here I check if counter is less than 2, if so, keep executing the timeline.
        return true
      }
    }
  }

    timeline.push(if_node);

    /* start the experiment */
    jsPsych.init({
      timeline: timeline,
      on_finish: function() {
        jsPsych.data.displayData();
      }
    });
</script>
</html>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60224151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档