javascript数组提示中有多个正确答案?
所以我有一个功能,基本上是问一个问题,需要正确的答案,直到标点符号;我不要这个。有没有办法将其编码为允许“这些答案中的任何一个”?比如“是的,当然,绝对是的,嗯,和/或该死的是的”而不是“是的” 我的职能:javascript数组提示中有多个正确答案?,javascript,arrays,Javascript,Arrays,所以我有一个功能,基本上是问一个问题,需要正确的答案,直到标点符号;我不要这个。有没有办法将其编码为允许“这些答案中的任何一个”?比如“是的,当然,绝对是的,嗯,和/或该死的是的”而不是“是的” 我的职能: function welcome() { var questions = [{ question: 'Are you ready to play?', answer: 'yes', affirm: 'Yay! You will be p
function welcome() {
var questions = [{
question: 'Are you ready to play?',
answer: 'yes',
affirm: 'Yay! You will be presented with a series of questions. If you answer a questions incorrectly, you cannot advance to the next...',
rebuttal: "No, you're definitely ready to play."
}];
for (var i = 0, l = questions.length; i < l; i++) {
answer = prompt(questions[i].question);
var correct = false;
while (correct === false)
if (answer !== questions[i].answer) {
alert(questions[i].rebuttal);
answer = prompt(questions[i].question);
} else {
alert(questions[i].affirm);
correct = true;
}
}
}
函数欢迎(){
变量问题=[{
问题:“你准备好玩了吗?”,
回答:“是的”,
确认:“耶!你会被问到一系列问题。如果你回答错误,你就不能进入下一个…”,
反驳:“不,你肯定已经准备好比赛了。”
}];
for(变量i=0,l=questions.length;i
更改
answer: 'yes'
到
或者创建一个答案变量并使用它。根据正确答案的性质,有几种可能性。我会解释其中两项: 1)一系列可能的答案 创建一个包含所有可能正确答案的数组,然后对照该数组检查提示结果:
var correctAnswers = ['yeah', 'ok', 'sure'];
var answer = prompt('Do you...?');
for (var i = 0; i<correctAnswers.length; i++) {
if (answer === correctAnswers[i])
result = true;
}
if (result) {
// acceptable answer code
} else {
// unacceptable answer code
}
3)两者的结合
没有什么可以阻止您创建一个regexp数组,只需依次匹配每个regexp即可。您可以创建一个可接受答案的列表,并根据每个答案检查您收到的答案 此外,您可以在比较之前将所有内容转换为大写或小写,以消除大小写差异、去除标点符号、将数字(如
one
转换为1
)以及您希望的任何其他转换
如果你想要一个能理解人类语言中任何口语或辩证版本含义的函数,你可能需要很长时间的编码。你的问题提出了“是”的六种选择。一点点努力可能会产生六个以上的结果,而你对“不”也有同样的问题。我可以继续…如果你需要使用
prompt()
你将不得不决定一个错误幅度,因为你不会涵盖所有的可能性,即使有一个很长的列表。否则,您可以将prompt()
替换为confirm()
,因为这将返回true
或false
,这更容易处理。我强烈建议使用confirm()
,但是如果需要prompt()
,下面是一些选项:
特定选项的数组
由于组合非常有限,所以不是很好,更多的可能性会产生更大的数组,此外,您可能最终需要一个多填充索引(除非添加自定义循环)
兽性正则表达式
更灵活,缩短代码以获得更多选项,但与直接字符串比较相比,性能问题可能较小
var questions = [{
question: 'Are you ready to play?',
answer: /((hell[sz]? )?ye[psa]h?|absolutely|duh|of course)!*/i, //matches "hell yea","hells yea","hellz yea","hell yeah","yep","yes","hellz yeah!","hell yesh" .... etc plus the full words at the end
affirm: 'Yay! You will be presented with a series of questions. If you answer a questions incorrectly, you cannot advance to the next...',
rebuttal: "No, you're definitely ready to play."
}];
if(questions[i].answer.test(answer))
正则表达式数组
合并了前2个问题,但使正则表达式更易于管理
var questions = [{
question: 'Are you ready to play?',
answers: [/ye[psa]!*/,/(hell )?yeah?!*/,/absolutely!*/,/duh!*/,/of course!*/,/yeehaw!*/]
affirm: 'Yay! You will be presented with a series of questions. If you answer a questions incorrectly, you cannot advance to the next...',
rebuttal: "No, you're definitely ready to play."
}];
var saidYes = false;
for(var j=0,c=questions[i].answers.length;j<c;j++)
{
if(questions[i].answers[j].test(answer))
{
saidYes = true;
break;
}
}
你可以做一个
['yes','yea','yeehaw']
等数组,然后检查答案.indexOf(userinput)>-1
-或者按照/(hell)?ye[psa]h?|当然/
创建一个正则表达式。取决于你想/不想或只是使用的具体程度——这可能要容易得多!我尝试了这一点,因为它需要最少的修改(我知道这可能不是最好的,但实际上只有少数几个答案,我会寻找这个目的)。。。然而,上述措施并不奏效。
var questions = [{
question: 'Are you ready to play?',
answers: ['yep','yes','yea','yeah','hell yeah','hell yea','absolutely','duh','of course'],
affirm: 'Yay! You will be presented with a series of questions. If you answer a questions incorrectly, you cannot advance to the next...',
rebuttal: "No, you're definitely ready to play."
}];
if(questions[i].answers.indexOf(answer) > -1) //.indexOf may need polyfill depending on supported browsers
var questions = [{
question: 'Are you ready to play?',
answer: /((hell[sz]? )?ye[psa]h?|absolutely|duh|of course)!*/i, //matches "hell yea","hells yea","hellz yea","hell yeah","yep","yes","hellz yeah!","hell yesh" .... etc plus the full words at the end
affirm: 'Yay! You will be presented with a series of questions. If you answer a questions incorrectly, you cannot advance to the next...',
rebuttal: "No, you're definitely ready to play."
}];
if(questions[i].answer.test(answer))
var questions = [{
question: 'Are you ready to play?',
answers: [/ye[psa]!*/,/(hell )?yeah?!*/,/absolutely!*/,/duh!*/,/of course!*/,/yeehaw!*/]
affirm: 'Yay! You will be presented with a series of questions. If you answer a questions incorrectly, you cannot advance to the next...',
rebuttal: "No, you're definitely ready to play."
}];
var saidYes = false;
for(var j=0,c=questions[i].answers.length;j<c;j++)
{
if(questions[i].answers[j].test(answer))
{
saidYes = true;
break;
}
}
var answer = confirm(questions[i].question);
var correct = false;
while (correct === false)
{
if(answer)
{
alert(questions[i].affirm);
correct = true;
}
else
{
alert(questions[i].rebuttal);
answer = confirm(questions[i].question);
}
}