当str设置为opt2时,为什么分数会评估分数+1

mari

我有一个函数,可以根据选择的选项来计算调查中的分数,但是如果条件变量问题= [{“ question”:“ L'impest est”,option1:“,我总是会获得相同的值faible”,option2:“ moyen”,option3:“ haut”,},

var currentQuestion = 0;
var score1 = 0;
var totQuestions = questions.length;
var number = 0;


var progressText = document.getElementById("progressText");
var container = document.getElementById('quizContainer');
var questionEl = document.getElementById('question');
var opt1 = document.getElementById('opt1');
var opt2 = document.getElementById('opt2');
var opt3 = document.getElementById('opt3');
var nextButton = document.getElementById('nextButton');
var resultCont = document.getElementById('result');



function loadQuestion(questionIndex) {
    var q = questions[questionIndex];
    questionEl.textContent = (questionIndex + 1) + '. ' + q.question;
    opt1.textContent = q.option1;
    opt2.textContent = q.option2;
    opt3.textContent = q.option3;
    progressText.innerText = questionIndex + 1 + ' de ' + totQuestions + ' 
questions ';
 };

  function loadNextQuestion() {
    var selectedOption = 
document.querySelector('input[type=radio]:checked');
    if (!selectedOption) {
        alert('veuillez sélectionner votre réponse');
        return;
    }

        var answer =selectedOption.Value;

        if (questions[currentQuestion].opt1 == answer) {
            score1 += 1;
        } else if (questions[currentQuestion].opt2 == answer) {
            score1 += 2;
        } else {
             score1 += 3
    }


    selectedOption.checked = false;
    currentQuestion++;
    progressText.textContent = questionEl / totQuestions

    if (currentQuestion == totQuestions - 1) {
        nextButton.textContent = 'Finish';
    }
    if (currentQuestion == totQuestions) {
        container.style.display = 'none';
        resultCont.style.display = '';
        resultCont.textContent = 'le résultat est ' + ' ' + score1 + ' ' 
+answer+' '+ option1 ;
        return;
    }
    loadQuestion(currentQuestion);
};
loadQuestion(currentQuestion);
马姆杜·赛义德(Mamdouh Saeed)

如果我对您的了解很好,那么您的问题可能是由于以下另一个原因:

  1. 您不会在<select>元素上触发更改事件
  2. answer的价值,您可能需要使用.selectedIndex获得的回答值。

看看onchange事件.selectedIndex

var questions = [{
  opt1: "Answer 1",
  opt2: "Answer 2",
  opt3: "Answer 3"
}];

// score1, currentQuestion variables defined as example
var currentQuestion = 0;
var score1 = 1;

var sel = document.getElementById("sel");

sel.addEventListener("change", function() {
  var answer = this.options[this.selectedIndex].value;
  if (questions[currentQuestion].opt1 == answer) {
    score1 += 1;
  } else if (questions[currentQuestion].opt2 == answer) {
    score1 += 2;
  } else {
    score1 += 3;
  }
  console.log("score1 = " + score1)
});
<div id="q"></div>
<select id="sel">
  <option value="Answer 1">Answer 1</option>
  <option value="Answer 2">Answer 2</option>
  <option value="Answer 3">Answer 3</option>
</select>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么isinstance([1,2,3],List [str])评估为true?

为什么1 ... 1评估为10.1?

为什么(1 == 2!= 3)在Python中评估为False?

为什么“ range(2)== True中的1”评估为False?

在C中,为什么将幂设置为1/2时幂函数返回1?

为什么`1> undefined`评估为false?

为什么语句[1,2] <[2,1]在python中评估为True

评估返回-1是否为TRUE?(为什么不是1)

如何使用代码块使用以下语法为值分配值:value =(bool)?opt1:opt2; 在Objective-C中

如何在wxMaxima中为所有类型的分数设置tex1输出?

为什么1 === 1 === 1为假?

为什么-1 >> 1计算为-1?

为什么HTML范围栏的最大值设置为1时,其值大于1?

为什么sklearns分类报告中的“加权”平均F1分数与通过公式计算出的F1分数不同?

为什么将我的两个JButton设置为1时要占用2个空格(网格宽度)?

逻辑表达式:为什么“str1 in str2 或 str2 not in str1”为我的打印语句返回 True?

在相同数据和相同算法上进行评估时,为什么会产生两个不同的AUC分数

将并发限制设置为 1 时,单个请求会旋转 2 个 Knative 实例

在JavaScript中将视频播放速率属性从1设置为2,从1设置为0.5时,究竟发生了什么变化?

为什么Array(1).join('str')输出为空?

为什么IDIV为-1会导致浮点异常?

为什么([1,0]中的1 = = True)评估为False?

Excel 如何将分数保留为分数 5/5 不是 1 或 0/5 不是 0

为什么是'if(1 == 1 == 1 == 1 == 1);' 正确,但'if(-1 == -1 == -1 == -1 == -1);' 假?

为什么1 =='1,2'为真?

为什么在JavaScript中,`2 << -1'为零而不是1?

为什么 str([1]) = [1] 而不是 Python 中的“1”

为什么将print(“ text” + str(var1)+“ more text” + str(var2))描述为“已拒登”?

为什么在JavaScript中“ 1” + +“ 1” =“ 11”但“ 1”--“ 1” = 2