Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将javascript测验结果更改为基于权重的评分?_Javascript - Fatal编程技术网

如何将javascript测验结果更改为基于权重的评分?

如何将javascript测验结果更改为基于权重的评分?,javascript,Javascript,我正在尝试使用javascript构建一个多项选择测试,该测试一次显示一个问题(淡入下一个问题)。在回答完所有问题后,根据您的回答,它会给出您的结果。实际问题的结果不需要仅仅显示一个分数和分数的解释,因为结果将是一堆文本 例如: 0-20分=结果1 21-50=结果2 51-60=结果3 61-80=结果4 81-100=结果5 下面是我的代码,我如何更改它,使没有答案是“正确的”,它只是在测验结束时根据上述分数范围给出一个文本结果 Javascript: (function() { va

我正在尝试使用javascript构建一个多项选择测试,该测试一次显示一个问题(淡入下一个问题)。在回答完所有问题后,根据您的回答,它会给出您的结果。实际问题的结果不需要仅仅显示一个分数和分数的解释,因为结果将是一堆文本

例如:

  • 0-20分=结果1
  • 21-50=结果2
  • 51-60=结果3
  • 61-80=结果4
  • 81-100=结果5
下面是我的代码,我如何更改它,使没有答案是“正确的”,它只是在测验结束时根据上述分数范围给出一个文本结果

Javascript:

 (function() {
var questions = [{
question: "Question1",
choices: ["choice1", "choice2", "choice3"],
correctAnswer:0
}, {
question: "Question2",
choices: ["choice1", "choice2", "choice3"],
correctAnswer: 4
}, {
question: "Question3",
choices: ["choice1", "choice2", "choice3"],
correctAnswer: 0
}, {
question: "Question4",
choices: ["choice1", "choice2", "choice3"]
correctAnswer: 3
}, {
question: "Question5",
choices: ["choice1", "choice2", "choice3"],
correctAnswer: 4
}];

var questionCounter = 0; //Tracks question number
var selections = []; //Array containing user choices
var quiz = $('#quiz'); //Quiz div object

// Display initial question
displayNext();

// Click handler for the 'next' button
$('#next').on('click', function (e) {
e.preventDefault();

// Suspend click listener during fade animation
if(quiz.is(':animated')) {        
  return false;
}
choose();

// If no user selection, progress is stopped
if (isNaN(selections[questionCounter])) {
  alert('Please make a selection!');
} else {
  questionCounter++;
  displayNext();
}
});

// Click handler for the 'prev' button
$('#prev').on('click', function (e) {
e.preventDefault();

if(quiz.is(':animated')) {
  return false;
}
choose();
questionCounter--;
displayNext();
});

// Click handler for the 'Start Over' button
$('#start').on('click', function (e) {
e.preventDefault();

if(quiz.is(':animated')) {
  return false;
}
questionCounter = 0;
selections = [];
displayNext();
$('#start').hide();
});

 // Animates buttons on hover
 $('.button').on('mouseenter', function () {
 $(this).addClass('active');
 });
 $('.button').on('mouseleave', function () {
 $(this).removeClass('active');
 });

  // Creates and returns the div that contains the questions and 
  // the answer selections
  function createQuestionElement(index) {
  var qElement = $('<div>', {
  id: 'question'
  });

  var header = $('<h2>Question ' + (index + 1) + '</h2>');
  qElement.append(header);

  var question = $('<p>').append(questions[index].question);
  qElement.append(question);

  var radioButtons = createRadios(index);
  qElement.append(radioButtons);

  return qElement;
  }

  // Creates a list of the answer choices as radio inputs
  function createRadios(index) {
  var radioList = $('<ul>');
  var item;
  var input = '';
  for (var i = 0; i < questions[index].choices.length; i++) {
  item = $('<li>');
  input = '<input type="radio" name="answer" value=' + i + ' />';
  input += questions[index].choices[i];
  item.append(input);
  radioList.append(item);
   }
   return radioList;
   }

   // Reads the user selection and pushes the value to an array
   function choose() {
   selections[questionCounter] = +$('input[name="answer"]:checked').val();
   }

   // Displays next requested element
    function displayNext() {
    quiz.fadeOut(function() {
    $('#question').remove();

    if(questionCounter < questions.length){
    var nextQuestion = createQuestionElement(questionCounter);
    quiz.append(nextQuestion).fadeIn();
    if (!(isNaN(selections[questionCounter]))) {
     $('input[value='+selections[questionCounter]+']').prop('checked', true);
    }

    // Controls display of 'prev' button
    if(questionCounter === 1){
      $('#prev').show();
    } else if(questionCounter === 0){

      $('#prev').hide();
      $('#next').show();
    }
  }else {
    var scoreElem = displayScore();
    quiz.append(scoreElem).fadeIn();
    $('#next').hide();
    $('#prev').hide();
    $('#start').show();
  }
  });
  }

  // Computes score and returns a paragraph element to be displayed
  function displayScore() {
  var score = $('<p>',{id: 'question'});

  var numCorrect = 0;
  for (var i = 0; i < selections.length; i++) {
  if (selections[i] === questions[i].correctAnswer) {
    numCorrect++;
  }
}

score.append('You got ' + numCorrect + ' questions out of ' +
             questions.length + ' right!!!');
return score;
}
})();
(函数(){
变量问题=[{
问题:“问题1”,
选项:[“选项1”、“选项2”、“选项3”],
正确答案:0
}, {
问题:“问题2”,
选项:[“选项1”、“选项2”、“选项3”],
正确答案:4
}, {
问题:“问题3”,
选项:[“选项1”、“选项2”、“选项3”],
正确答案:0
}, {
问题:“问题4”,
选项:[“选项1”、“选项2”、“选项3”]
正确答案:3
}, {
问题:“问题5”,
选项:[“选项1”、“选项2”、“选项3”],
正确答案:4
}];
var questionCounter=0;//跟踪问题编号
var selections=[];//包含用户选择的数组
var quick=$('#quick');//quick div对象
//显示初始问题
displayNext();
//单击“下一步”按钮的处理程序
$(“#下一步”)。在('click',函数(e){
e、 预防默认值();
//在淡入淡出动画期间暂停单击侦听器
如果(quick.is(':animated'){
返回false;
}
选择();
//如果没有用户选择,则停止进度
if(isNaN(选择[问询计数器]){
警报('请选择!');
}否则{
问句器++;
displayNext();
}
});
//单击“prev”按钮的处理程序
$('prev')。在('click',函数(e)上{
e、 预防默认值();
if(quick.is(':animated')){
返回false;
}
选择();
问讯台--;
displayNext();
});
//单击“重新开始”按钮的处理程序
$('#start')。在('click',函数(e)上{
e、 预防默认值();
if(quick.is(':animated')){
返回false;
}
问讯员=0;
选择=[];
displayNext();
$('#start').hide();
});
//设置悬停按钮的动画
$('.button')。在('mouseenter',函数(){
$(this.addClass('active');
});
$('.button')。打开('mouseleave',函数(){
$(this.removeClass('active');
});
//创建并返回包含问题和答案的div
//答案选择
函数createQuestionElement(索引){
变量qElement=$(''{
id:“问题”
});
变量头=$('Question'+(索引+1)+'');
追加(标题);
var question=$(“”).append(问题[index].question);
附加(问题);
var radioButtons=createRadios(索引);
qElement.append(单选按钮);
返回元素;
}
//创建答案选项列表作为无线电输入
函数createRadios(索引){
var放射科医生=$(“
    ”); var项目; var输入=“”; 对于(var i=0;i”); 输入=''; 输入+=问题[索引]。选项[i]; 项目。追加(输入); 放射科医生。追加(项目); } 返回放射科医生; } //读取用户选择并将值推送到数组 函数选择(){ 选择[questionCounter]=+$('input[name=“answer”]:选中')。val(); } //显示下一个请求的元素 函数displayNext(){ 测验.淡出(函数(){ $(“#问题”).remove(); if(问询台
HTML:


关系检验

我用叉子叉了你的笔: 我想我已经修好了

请看答案,这些答案对选择有价值。 函数的作用是:计算这一行,特别是这一行

score = score + quiz[currentQuestion].answer[i];

输出在最后,仅显示一个单数分数。

以下是您的问题的回答在确定0-100范围内的最终结果时如何具有“权重”以及介于两者之间的内容:

var questions = [
    {
        question: "Question1",
        choices: [
                    "choice1",
                    "choice2",
                    "choice3"
                    ],
        weights: [10, 7, 3]
    },
    {
        question: "Question2",
        choices: [
                    "choice1",
                    "choice2",
                    "choice3"
                    ],
        weights: [3, 10, 7]
    },
    {
        question: "Question3",
        choices: [
                    "choice1":10,
                    "choice2":0,
                    "choice3":10
                    ],
        weights: [10, 0, 10]
    },
    {
        question: "Question4",
        choices: [
                    "choice1",
                    "choice2",
                    "choice3"
                    ],
        weights: [3, 10, 7]
    },
    {
        question: "Question5",
        choices: [
                    "choice1",
                    "choice2",
                    "choice3"
                    ],
        weights: [10, 3, 7]
    }
];
我所做的是取尽可能高的分数,即100分,除以我给出的问题数,在这种情况下是5分,然后将每个问题的20分在答案之间不相等(这很重要),给“最佳”答案更多的分,给“最差”答案更少或0分

注意,我总是确保任何给定问题的所有分数都等于20,这样每个问题的选择在确定分数时都是“公平的”。我希望这能解决你问题中“没有正确答案”的部分

更新:我已经更新了代码,以便更轻松地处理答案

在“新”版本中,每个问题都有一个“权重”数组,权重数字应该分别对应于每个“选择”。在你的申请中,每个问题都应该是pr
var questions = [
    {
        question: "Question1",
        choices: [
                    "choice1",
                    "choice2",
                    "choice3"
                    ],
        weights: [10, 7, 3]
    },
    {
        question: "Question2",
        choices: [
                    "choice1",
                    "choice2",
                    "choice3"
                    ],
        weights: [3, 10, 7]
    },
    {
        question: "Question3",
        choices: [
                    "choice1":10,
                    "choice2":0,
                    "choice3":10
                    ],
        weights: [10, 0, 10]
    },
    {
        question: "Question4",
        choices: [
                    "choice1",
                    "choice2",
                    "choice3"
                    ],
        weights: [3, 10, 7]
    },
    {
        question: "Question5",
        choices: [
                    "choice1",
                    "choice2",
                    "choice3"
                    ],
        weights: [10, 3, 7]
    }
];
<select id="response">
    <option value="10">Best answer</option>
    <option value="7">Moderate answer</option>
    <option value="3">Worst answer</option>
</select>
var total = 0;
for (var i = 0; i < selections.length; i++) {
    total += selections[i];
}