我在 array 中存储了几个问题var questions=[]。我正在使用forEach遍历数组并为每个问题输入输入并将其显示在终端本身上。但它只询问第一个问题,显示响应并停留在那里。它不会转移到下一个问题。我应该使用rl.close(),但在哪里。这是我的代码Quiz.js。
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var questions=[
"Hi, What is your name?",
"I need your contact number also",
"Thanks! What is your favourite color?"
];
questions.forEach(myFunction);
function myFunction(item, index) {
rl.question(item, (answer) => {
console.log(`You said: ${answer}`);
});
rl.close(); //THIS IS IMMEDIATELY CLOSING AFTER THE FIRST QUESTION
}
请纠正我。
萧十郎
MMTTMM
相关分类