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

时间:2019-04-22 09:00:07

标签: javascript

我有一个函数,可以根据所选选项计算调查中的分数,但是我总是得到与第一个if条件相同的值,我猜答案变量就是问题所在,因为它什么也没显示。

var questions = [
    {
        "question": "L'impact est ",
        "option1": "faible",
        "option2": "moyen",
        "option3": "haut",
    },
    {
       "question": "L'impact2 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');
var chartBar = document.getElementById('bar-horzontal');

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+' ' ;
        return;
     }
     loadQuestion(currentQuestion);
 };
 loadQuestion(currentQuestion);

2 个答案:

答案 0 :(得分:0)

我认为您是在告诉程序向选项2和3添加额外的点。请看下面的代码,如果选项2是添加2点,选项3是3点。

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

我认为他们应该像这样。

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

答案 1 :(得分:0)

在HTML中,input元素没有属性textContent,因此您不应该在radio元素内添加文本,可以使用span或{{1} }。

label也是q.opt1

undefined
var questions = [{
    "question": "L'impact est",
    "option1": "faible",
    "option2": "moyen",
    "option3": "haut"
  },
  {
    "question": "L'impact 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 lbl_1 = document.getElementById('lbl_1');
var lbl_2 = document.getElementById('lbl_2');
var lbl_3 = document.getElementById('lbl_3');
var nextButton = document.getElementById('nextButton');
var resultCont = document.getElementById('result');
var chartBar = document.getElementById('bar-horzontal');

function loadQuestion(questionIndex) {
  var q = questions[questionIndex];
  questionEl.textContent = (questionIndex + 1) + '. ' + q.question;
  opt1.value = lbl_1.textContent = q.option1;
  opt2.value = lbl_2.textContent = q.option2;
  opt3.value = lbl_3.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].option1 == answer) {
    score1 += 1;
  } else if (questions[currentQuestion].option2 == answer) {
    score1 += 2;
  } else {
    score1 += 3
  }

  selectedOption.checked = false;
  currentQuestion++;
  progressText.textContent = "Answered: "+(currentQuestion / totQuestions) * 100 + "%";

  if (currentQuestion == totQuestions - 1) {
    nextButton.textContent = 'Finish';
  }
  if (currentQuestion == totQuestions) {
    nextButton.style.display = 'none';
    container.style.display = 'none';
    resultCont.style.display = '';
    resultCont.textContent = 'le résultat est ' + ' ' + score1 + ' ';
    return;
  }
  loadQuestion(currentQuestion);
};
loadQuestion(currentQuestion);