2017-05-06 1 views
1

質問のリストがありますが、これは順次表示したいものです。現在のコードは質問をランダムな順序で表示します。すべての質問を順番に表示するのを助けてくれますか?ここでは、コードは次のとおりです。質問のリストを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(); 

ありがとうございました。

+0

あなただけの、最後に表示された質問インデックスの数を維持し、次のいずれかが表示され、カウントをインクリメントし、代わりに – ADyson

答えて

0

あなたはグローバルindex変数を使用し、長さよりも小さいながら、それをインクリメントすることができます。

function questionconstructor(question, answers, correctanswer) { 
 
    this.question = question; 
 
    this.answers = answers; 
 
    this.correctanswer = correctanswer; 
 
} 
 

 
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() { 
 
    questions[index].displayquestion(); 
 
    var answerofperson = prompt(questions[index].question + ' (' + questions[index].answers.map(function (a, i) { return [i, a].join(': '); }).join(', ') + '):') 
 
    if (answerofperson !== 'exit') { 
 
     questions[index].checkanswer(parseInt(answerofperson)); 
 
     index++; 
 
     if (index < questions.length) { 
 
      displaynextque(); 
 
     } 
 
    } 
 
} 
 

 
var q1 = new questionconstructor('what do you want to do?', ['Job', 'WFH', 'Business'], 2), 
 
    q2 = new questionconstructor('Where do you want to go?', ['USA', 'UK', 'JAP'], 1), 
 
    q3 = new questionconstructor('Which car do you use?', ['Audi', 'BMW', 'Honda'], 0), 
 
    questions = [q1, q2, q3], 
 
    index = 0; 
 

 
displaynextque();
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

ありがとうございました@Nina Scholz – Sushant

0

はこれに最後の関数を変更してみてください:

function displaynextque(n){ 
var count = n ? n : 0; 

questions[count].displayquestion(); 
var answerofperson = prompt("please select your answer!") 
if (answerofperson !== 'exit'){ 
questions[count].checkanswer(parseInt(answerofperson)); 
if(count < questions.length - 1) { 
displaynextque(count+1); 
} 
} 
} 
displaynextque(); 
+0

はあなたに@Patrik Torkidsenに感謝するたびに乱数を生成する必要があります。..それも動作します! – Sushant