javascript数组提示中的多个真实答案?

时间:2013-10-15 15:16:40

标签: javascript arrays

所以我有一个功能,基本上问一个问题,并要求正确的答案到标点符号;我不想要这个。有没有办法在它允许“任何这些答案”的地方编码?就像“是的,当然,绝对,呃,和/或地狱是的”而不仅仅是“是”?

我的功能:

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

4 个答案:

答案 0 :(得分:1)

根据您正确答案的性质,有几种可能性。我将解释其中两个:

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
}

PS:您可以使用indexOf查看某个值是否在数组中,但这不适用于下面的3)。

2)回答RegExp

如果你想捕捉相同答案的变体,这个解决方案比第一个更好:

var correctAnswer = /[yY]es(s?)/; // catches yes, Yes, Yess, yess

var answer = prompt('Do you...?');
if (answer.match(correctAnswer)){
  // acceptable answer code
} else {
  // unacceptable answer code
}

3)两者的结合

没有什么可以阻止你创建一个regexp数组,只是依次匹配它们。

答案 1 :(得分:0)

您可以创建一个可接受的答案列表,并检查每个答案的结果。

此外,您可以在比较之前将所有内容转换为大写或小写,以消除大小写差异,剥离标点符号,将one等数字转换为1,以及您希望的任何其他转换。

如果你要求的功能可以理解人类语言中任何口语或辩证版本的含义,那么你可能需要很长时间的编码。你的问题提出了“是”的六种选择。一点点的努力可能会产生六个,并且你有'不'的同样问题。我可以继续......

答案 2 :(得分:0)

如果您需要使用prompt(),则必须确定错误边距,因为不会涵盖所有可能性,即使是一个非常长的列表。否则,您只需将prompt()替换为confirm(),因为这会让您回复truefalse,这样会更容易处理。我强烈建议您使用confirm(),但如果需要prompt(),则有以下选项:

特定选项的数组

由于组合非常有限,不是很好,更多的可能性会产生更大的阵列,而且你可能最终需要一个indexOf polyfill(除非你添加一个自定义循环)

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

正则数组

结合前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;
    }
}

使用确认() - 个人推荐

通过消除不同反应的可能性来简化整个过程

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

答案 3 :(得分:-1)

更改

answer: 'yes'

answer:[' y e s ' ,'of course', 'absolutely', 'duh', 'hell yeah']

或者创建答案的变量并使用它。