Не отображается счет в приложении викторины JavaScript? - PullRequest
0 голосов
/ 07 мая 2020

Я только что попытался создать приложение JavaScript викторины и включить отображение оценки за викторину. Я дам JavaScript ниже, пожалуйста, проверьте:

var questions = [];
$.ajax({
  url: 'http://127.0.0.1:8000/api/?format=json',
  type: 'GET',
  async: true,
  dataType: "json",
  success: function(data) {
    questions = data;
    loadQuestion();

  }
});

function loadQuestion() {
  //questionIndex = 0;
  var questionEl = document.getElementById("question");
  var opt1 = document.getElementById("opt1");
  var opt2 = document.getElementById("opt2");
  var opt3 = document.getElementById("opt3");
  var opt4 = document.getElementById("opt4");

  questionEl.innerHTML = (currentQuestion + 1) + '. ' + questions[currentQuestion].question;
  opt1.innerHTML = questions[currentQuestion].option1;
  opt2.innerHTML = questions[currentQuestion].option2;
  opt3.innerHTML = questions[currentQuestion].option3;
  opt4.innerHTML = questions[currentQuestion].option4;
}
var currentQuestion = 0;
var score = 0;
var totQuestions = questions.length;
var container = document.getElementById('quizContainer');
var nextButton = document.getElementById('nextButton');
var resultCont = document.getElementById('result');


function loadNextQuestion() {

  var selectedOption = document.querySelector('input[type=radio]:checked');
  if (!selectedOption) {
    alert('Please select your answer!');
    return;
  }
  var answer = selectedOption.value;
  if (questions[currentQuestion].correct_answer == answer) {
    score += 10;
  }

  selectedOption.checked = false;
  currentQuestion++;


  if (currentQuestion == totQuestions - 1) {
    nextButton.textContent = 'Finish';
  }


  if (currentQuestion == totQuestions) {
    container.style.display = 'none';
    resultCont.style.display = '';
    resultCont.textContent = 'Your Score: ' + score;
    return;
  }
  loadQuestion(currentQuestion);
}

loadQuestion(currentQuestion);

Теперь проблема заключалась в том, что не отображалась последняя кнопка fini sh, а счет не отображался, а ошибка

Uncaught TypeError: Невозможно прочитать свойство «правильный_ответ» неопределенного

Неперехваченное TypeError: Невозможно прочитать свойство «question» неопределенного значения

Это ошибки. Не могли бы вы исправить это?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...