0

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

我的功能:

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

4 回答 4

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)答案的正则表达式

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

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) 两者结合

没有什么能阻止您创建一个正则表达式数组并简单地依次匹配每个正则表达式。

于 2013-10-15T15:25:17.743 回答
0

如果您需要使用prompt(),则必须确定误差范围,因为即使列表很长,您也不会涵盖所有可能性。否则,您可以替换为prompt()confirm()因为这会给您带来回报true,或者false更容易处理。我强烈建议使用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;
    }
}

使用confirm()——个人推荐

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

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);
    }
}
于 2013-10-15T15:38:49.727 回答
0

您可以创建一个可接受答案的列表,并对照每个答案检查您收到的内容。

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

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

于 2013-10-15T15:26:22.003 回答
-1

改变

answer: 'yes'

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

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

于 2013-10-15T15:24:03.670 回答