JavaScript Switch语句-单个案例中的同义词

时间:2018-09-01 18:39:01

标签: javascript switch-statement case

我正在尝试设计一个页面,向用户询问几个问题,然后在文本框中输入答案。

我正在使用Switch语句针对不同的答案生成不同的注释。

可以用许多不同的方式键入相同的答案。例如,用户可能会输入“是”或“您打赌”而不是输入“是”。

有没有一种方法可以将所有可能的答案都视为一件事情,从而避免发生以下情况:

switch (answers) {
    case "Yes":
    case "Yep":
    case "Yeah":
    case "You bet":
    case "Sure":
    case "Absolutely":
        text = "Me too.";
        break;
    case "No":
    case "Nope":
    case "Nah":
    case "I hate it":
    case "I do not":
    case "Not at all":
        text = "Why not?";
        break;
    default:
        text = "What?"

我想要一个单独的文件,其中包含页面中所有预期答案的所有可能的同义词。

这样,我可能会有类似的内容,即使用户输入“ You bet”或“ Yeah”,它也会显示“是”的注释,而不是“默认”的注释,或其他任何同义词:

    switch (answers) {
    case "Yes":
        text = "Me too.";
        break;
    case "No":
        text = "Why not?";
        break;
    default:
        text = "What?"

这就是我正在使用的:

function myFunction() {
    var text;
    var answers = document.getElementById("myInput").value;
    switch (answers) {
        case "Yes":
        case "Yep":
        case "Yeah":
        case "You bet":
        case "Sure":
        case "Absolutely":
            text = "Me too.";
            break;
        case "No":
        case "Nope":
        case "Nah":
        case "I hate it":
        case "I do not":
        case "Not at all":
            text = "Why not?";
            break;
        default:
            text = "What?"
    }
    document.getElementById("comment").innerHTML = text;
}
<p>Do you like waffles?</p>
<input id="myInput" type="text">
<button onclick="myFunction()">Answer</button>
<p id="comment"></p>

3 个答案:

答案 0 :(得分:1)

这是一种可能的方法:

{
  const questions = [
    { 
      text: 'Do you like waffles?',
      inputs: {
        yes: ['yes', 'yep', 'yeah'],
        no: ['no', 'nope', 'nah']
      },
      replies: {
        yes: 'Me too.',
        no: 'Why not?',
        other: 'What?'
      }
    }, 
    { 
      text: 'Do you like pasta?',
      inputs: {
        yes: ['yes', 'yep', 'yeah'],
        no: ['no', 'nope', 'nah']
      },
      replies: {
        yes: 'Me too!',
        no: 'Why not?!',
        other: 'What?!'
      }
    }, 
  ];
  
  let currentQuestionIndex = -1;
  
  function goToNextQuestion() {
    let currentQuestion = questions[++currentQuestionIndex];
    document.getElementById('myQuestion').innerHTML = currentQuestion.text;
    document.getElementById('myInput').value = '';
    document.getElementById('myInput').focus();
  }
  
  function answer() {
    let currentQuestion = questions[currentQuestionIndex];
    const input = document.getElementById('myInput').value;
    const replyKey = Object.keys(currentQuestion.inputs)
      .find(inputKey => currentQuestion.inputs[inputKey].includes(input)) 
        || 'other';
    document.getElementById('answer').innerHTML = currentQuestion.replies[replyKey];
    if (currentQuestionIndex < questions.length - 1) {
      goToNextQuestion();
    }
  }
  
  goToNextQuestion();
}
<p id="myQuestion"></p>
<input id="myInput" type="text">
<button onclick="answer()">Answer</button>
<p id="answer"></p>

注意:附加的括弧括起来可防止用常量污染全局范围。不过需要ES6-如果可以使用全局变量,则可以摆脱它们。

答案 1 :(得分:0)

一个选项可能是拥有2个数组或集合,一个具有所有的Yes响应,另一个具有所有的No响应,并在进入switch语句之前检查输入是否与数组/集合中的任何值匹配。

答案 2 :(得分:0)

您可以使用一个对象和一个函数,使用其归一化(小写)的字符串来迭代答案和短语。

function x(answer) {
    var check = {
            answers: [
                {
                    phrases: ["yep", "yeah", "you bet", "sure", "absolutely"],
                    text: "Me too."
                },
                {
                    phrases: ["no", "nope", "nah", "i hate it", "i do not", "not at all"],
                    text: "Why not?"
                }
            ],
            text: "What?"
        };

    answer = answer.toLowerCase();
    return (check.answers.find(o => o.phrases.includes(answer.toLowerCase())) || check).text;
}

console.log(x('No'));
console.log(x('Sure'));
console.log(x('Whatever!'));