2017-12-13 6 views
1

これは簡単なことだと思われますが、ドキュメントで何も見つかりません。ノードjs rlが最後のプロンプトを繰り返す

質問したい質問があります。そのうちの1つは、有効な回答が得られるまで再質問します。このように:

rl.question('Author: ', function(answer) { //question #1 
    author = answer; //Use that value, move to next question 
    rl.question('What Title should be shown in browser tabs for this site? ', function(answer) { //question #2 
     title = answer; //Move on... 
     rl.question('Include the tippy.js library? ', function(answer) { //question #3 
      if (answer == 'y' || answer == 'yes' || answer == 'Yes' || answer == 'Y') { 
       console.log("Will include tippy.js"); 
       //Done with app 
      } else if (answer == 'n' || answer == 'no' || answer == 'N' || answer == 'No') { 
       console.log("Will not include tippy.js"); 
       //Done with app 
      } else { 
       console.log("Invalid response"); 
       //Re-ask question #3 without asking questions #1 and #2 
      } 
     }); 
    }) 
}); 

提案ですか?ご検討いただきありがとうございます。

+0

function shouldIncludeTippy() { rl.question('Include the tippy.js library? ', function(answer) { if (answer === 'y' ...) { console.log('Will include tippy.js'); } else if (answer === 'n' ...) { console.log('Will not include tippy.js'); } else { console.log('Invalid response'); shouldIncludeTippy(); } }); } 

次に、あなたのコードは次のように読み取ります。あなたの質問は何ですか? – AJFarmar

+0

私は、コメントが「#1と#2の質問をせずに質問3を再入力してください」と言う箇所を何にするべきかわかりません –

答えて

1

それを関数にスティックし、それを繰り返す必要があるたびにその関数を呼び出すことができます。あなたはすぐそこのソリューションを持っているように見えるまし

rl.question('Author: ', function(answer) { //question #1 
    author = answer; //Use that value, move to next question 
    rl.question('What Title should be shown in browser tabs for this site? ', function(answer) { //question #2 
     title = answer; //Move on... 
     shouldIncludeTippy(); 
    }) 
}); 
関連する問題