1

Je sais que cette question a peut-être été posée auparavant, mais je crois que cette communauté génial peut me pardonner pour cela :) Je développe l'application (Quiz time), mais il y a un bug qui est me tue :( Quand je lance cette application tous et appuyez sur le bouton de verrouillage, il agit comme voulu, mais après un nombre aléatoire de clics (peut-être 4 ou 2) il arrête et me donne cette erreur:Uncaught TypeError: Impossible de lire la propriété 'quest' de undefined

  Uncaught TypeError: Cannot read property 'quest' of undefined 
at QuestionReset 

est ici le code

 $(document).ready(function() { 
var overwallScore = 0; 
//prototype 
function Question(quest, option1, option2, option3, correct) { 
    this.quest = quest; 
    this.option1 = option1; 
    this.option2 = option2; 
    this.option3 = option3; 
    this.correct = correct; 

} 
//creating questions 
var question1 = new Question("1 + 1", 2, 3, 3, 1); 
var question2 = new Question("2 + 2", 4, 3, 3, 2); 
var question3 = new Question("3 + 3", 6, 3, 3, 3); 
var question4 = new Question("4 + 4", 8, 3, 3, 1); 
var question5 = new Question("5 + 5", 10, 3, 3, 2); 
var question6 = new Question("6 + 6", 12, 3, 3, 3); 
//the questions array 
var questions = []; 
//pushing questions to array 
questions.push(question1); 
questions.push(question2); 
questions.push(question3); 
questions.push(question4); 
questions.push(question5); 
questions.push(question6); 
var randomQuestionIndex = Math.floor(Math.random() * 6 + 1); 

function QuestionPre() { 
    randomQuestionIndex = Math.floor(Math.random() * 6 + 1); 
    document.querySelector("#question").innerHTML = questions[randomQuestionIndex].quest; 
    document.querySelector(".option-1").innerHTML = questions[randomQuestionIndex].option1; 
    document.querySelector(".option-2").innerHTML = questions[randomQuestionIndex].option2; 
    document.querySelector(".option-3").innerHTML = questions[randomQuestionIndex].option3; 

} 
QuestionPre(); 

function QuestionReset() { 
    randomQuestionIndex = Math.floor(Math.random() * 6 + 1); 
    document.querySelector("#question").innerHTML = questions[randomQuestionIndex].quest; 
    document.querySelector(".option-1").innerHTML = questions[randomQuestionIndex].option1; 
    document.querySelector(".option-2").innerHTML = questions[randomQuestionIndex].option2; 
    document.querySelector(".option-3").innerHTML = questions[randomQuestionIndex].option3; 
} 
document.querySelector(".lock-btn").addEventListener("click", function() { 
    if (document.querySelector(".option-" + questions[randomQuestionIndex].correct + "-check").checked === true) { 
     alert("Correct, such a Genius !"); 
     overwallScore = overwallScore + 1; 
     QuestionReset(); 
    } else { 
     alert("Wrong mate, such a bad luck !"); 
     randomQuestionIndex = Math.floor(Math.random() * 6 + 1); 
     QuestionReset(); 


    } 
}); }); 

Merci d'avance.

+0

'var randomQuestionIndex = Math.floor (Math.random() * 6 + 1);' la valeur de 'randomQuestionIndex' pourrait aller au-delà de la plage supprimer la partie' + 1' et il devrait fonctionner correctement. –

+0

CE MATE JUSTE TRAVAILLÉ MATE ET JE NE SAIS PAS POURQUOI MERCI DIEU DE L'ENVOYER À MOI, REMERCIE MATE CE JUSTE MA JOURNEE BEAUCOUP MEILLEUR! J'AI FAIT CECI POUR MON DIEU FRAPPE DOMMAGE DIEU MERCI ENCORE PLZ ÉCRIVEZ-LE COMME RÉPONSE SI JE PEUX MARQUER –

Répondre

0

Le problème se trouve dans la ligne

var randomQuestionIndex = Math.floor(Math.random() * 6 + 1);

La valeur de randomQuestionIndex peut aller hors de portée. Il suffit de supprimer la partie +1 et cela devrait fonctionner.

+0

Merci encore :) –