gpt4 book ai didi

javascript - Node js rl 重复上次提示

转载 作者:行者123 更新时间:2023-11-30 20:54:42 26 4
gpt4 key购买 nike

这看起来应该很容易做到,但我在文档中找不到任何内容。

我有一系列问题想问,其中一个我想重新问,直到我得到有效答案。像这样:

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
}
});
})
});

建议?感谢您的考虑。

最佳答案

您可以将它放在一个函数中,然后在每次需要重复时调用该函数。

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();
}
});
}

然后你的代码是这样的:

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();
})
});

关于javascript - Node js rl 重复上次提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47798577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com