質問のリストがありますが、これは順次表示したいものです。現在のコードは質問をランダムな順序で表示します。すべての質問を順番に表示するのを助けてくれますか?ここでは、コードは次のとおりです。質問のリストをJavascriptでインクリメンタルオーダーで表示
function questionconstructor(question, answers, correctanswer){
this.question = question;
this.answers = answers;
this.correctanswer = correctanswer;
}
var q1 = new questionconstructor('what do you want to do?', ['Job','WFH', 'Business'], 2);
var q2 = new questionconstructor('Where do you want to go?', ['USA', 'UK', 'JAP'], 1);
var q3 = new questionconstructor('Which car do you use?', ['Audi', 'BMW', 'Honda'], 0);
var questions = [q1, q2, q3];
questionconstructor.prototype.displayquestion = function(){
console.log(this.question);
for (var i=0; i < this.answers.length; i++){
console.log(i + ':' + this.answers[i]);
}
}
questionconstructor.prototype.checkanswer = function(ans){
if(ans === this.correctanswer){
console.log("Correct Answer!")
}else{
console.log("Try Again!")
}
}
function displaynextque(){
var rand = Math.floor(Math.random() * questions.length);
console.log(rand);
questions[rand].displayquestion();
var answerofperson = prompt("please select your answer!")
if (answerofperson !== 'exit'){
questions[rand].checkanswer(parseInt(answerofperson));
displaynextque();
}
}
displaynextque();
ありがとうございました。
あなただけの、最後に表示された質問インデックスの数を維持し、次のいずれかが表示され、カウントをインクリメントし、代わりに – ADyson