【问题标题】:JavaScript Switch Statement - Synonyms in a single CaseJavaScript Switch 语句 - 单个案例中的同义词
【发布时间】:2018-09-01 18:39:00
【问题描述】:

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

我正在使用 Switch 语句为不同的答案生成不同的 cmets。

可以用多种不同的方式输入相同的答案。例如,用户可能没有输入“yes”,而是输入“yeah”或“you bet”。

有没有办法将所有可能的答案视为一个单一的东西,以避免出现这样的情况:

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?"

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

那样,我可以有这样的东西,它会显示“是案例”的评论,而不是“默认”的评论,即使用户输入“你打赌”或“是的”,或任何其他同义词:

    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>

【问题讨论】:

    标签: javascript switch-statement case


    【解决方案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——如果你对全局变量没问题,你可以摆脱它们。

    【讨论】:

    • 感谢您的帮助。有没有办法让这些“常量输入”与文本框一起使用,而不是像您的建议那样使用窗口提示?我已经更新了我的帖子,添加了一个代码 sn-p 到目前为止我所拥有的。我可以让你的解决方案在那种环境下工作吗?
    • 再次感谢,我真的很抱歉打扰你,必须是显而易见的事情......但我还有另一个问题。我无法对这些输入进行持续回复,因为这些输入将用于以不同的回复回答其他问题。例如: - 你喜欢华夫饼吗? - 是的。 - 我也是。 -你知道我是谁吗? - 是的。 - 你知道吗?!”(相同的输入,不同的回复)这就是为什么我试图为每个问题提出一个 Switch 语句。如果用户键入预定数组的任何值,我希望它显示一个唯一的回复.
    • 你是想制作一个完整的聊天机器人,还是只是一个练习?如果是前者,您可能应该寻找现有的工具/库,其中有很多。如果是后者,那么在不了解您的全部要求的情况下很难为您提供帮助。您可能需要存储某种对话树,其中问题链接到其他问题(每个问题都有不同的可能答案)等等。
    • 这只是一个练习。我不需要太复杂的东西。我将有十页。每个人都会有一个问题供用户回答。一旦他给出了一个可接受的答案(其中一个 switch 案例),他将得到一个自定义回复,并将转到下一页/问题。不会有对话。每个页面/问题都将独立于其他页面/问题。
    • 查看我上次的编辑。我做的有点快,所以你可能需要清理一些东西,但希望这会有所帮助。
    【解决方案2】:

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

    【讨论】:

    • 感谢您的帮助。我还是个初学者。我研究过数组,所以我想我可以做那部分。但是如何让它检查输入是否与数组中的任何值匹配?
    【解决方案3】:

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

    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!'));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2021-03-22
      • 2014-03-15
      • 2010-10-23
      • 2014-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多