如何防止用户两次询问同一个问题?

问题描述 投票:2回答:4

如何阻止用户两次提出相同的问题?我已经拥有它所以他们必须用问号结束他们的问题。它只需要阻止他们连续两次询问同一个问题而不必永久。

function Response() {
  var answers = ["Ask again later...",
    "Yes",
    "No",
    "It appears to be so",
    "Reply is hazy, please try again",
    "Yes, definitely",
    "What is it you really want to know?",
    "Outlook is good",
    "My sources say no",
    "Signs point to yes",
    "Don't count on it",
    "Cannot predict now",
    "As i see it, yes",
    "Better not tell you now",
    "Concentrate ask again"
  ];

  var number = Math.floor(Math.random() * 15);

  if (document.getElementById("txtQuestion").value.indexOf("?") != -1) {
    document.getElementById("lblDisplay").innerHTML = answers[number];
  } else {
    alert("Please use a question mark at the end of the question!");
  }

}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Magic 8 Ball</title>
</head>

<body>
  <h1>Magic 8 Ball</h1>
  <h2>What would you like to know?</h2>
  <input id="txtQuestion" type="text" />
  <br /><br />
  <input type="button" value="Ask The 8 Ball" onclick="Response()" />
  <br /><br />
  <h3>The 8 Ball Says:</h3>
  <h4 id="lblDisplay">Ask the 8 ball a question...</h4>
</body>

</html>
javascript
4个回答
1
投票

这是一个非常深刻的问题。你如何定义一个问题是一样的?天真的,您可以简单地存储确切问题文本的哈希值和随机选择,这样如果他们确实再次询问相同的问题,他们将得到相同的答案。例如:

const store = {}

const answers = [
  "Ask again later...",
  "Yes",
  "No",
  "It appears to be so",
  "Reply is hazy, please try again",
  "Yes, definitely",
  "What is it you really want to know?",
  "Outlook is good",
  "My sources say no",
  "Signs point to yes",
  "Don't count on it",
  "Cannot predict now",
  "As i see it, yes",
  "Better not tell you now",
  "Concentrate ask again"
]

function Response() {
  const question = document.getElementById("txtQuestion").value
      
  if (question.indexOf("?") != -1) {        
    const number = store[question] || Math.floor(Math.random() * 15)  
    store[question] = number

    document.getElementById("lblDisplay").innerHTML = answers[number]
  } else {
    alert("Please use a question mark at the end of the question!")
  }
}
<h1>Magic 8 Ball</h1>
<h2>What would you like to know?</h2>

<input id="txtQuestion" type="text" />
<br /><br />

<input type="button" value="Ask The 8 Ball" onclick="Response()" />
<br /><br />

<h3>The 8 Ball Says:</h3>
<h4 id="lblDisplay">Ask the 8 ball a question...</h4>

但是,如果有人问'我明天会死吗?'并且'我明天会死吗?'

这些可以说是同一个问题,并且需要一个更加复杂的解决方案,您需要利用自然语言处理。这是一个涉及很多周围问题的巨大领域。例如:How to detect that two sentences are similar?


1
投票

为什么不保留所有问题?

var questions = {};
function Response() {
    var answers = ["Ask again later...",
    "Yes",
    "No",
    "It appears to be so",
    "Reply is hazy, please try again",
    "Yes, definitely",
    "What is it you really want to know?",
    "Outlook is good",
    "My sources say no",
    "Signs point to yes",
    "Don't count on it",
    "Cannot predict now",
    "As i see it, yes",
    "Better not tell you now",
    "Concentrate ask again"
    ];

    var number = Math.floor(Math.random() * 15);
    var q = document.getElementById("txtQuestion").value;
    if (q.indexOf("?") != -1) {
        if (!questions[q]){
            document.getElementById("lblDisplay").innerHTML = answers[number];
            questions[q] = true;
        } else {
            alert('I already answered that!');
        }
    } else {
    alert("Please use a question mark at the end of the question!");
    }

}

0
投票

将上一个问题保存在全局变量中并与当前变量进行比较

var lastQuestion = ''

function Response() {
  ...
  if (lastQuestion == document.getElementById("txtQuestion").value) {
    alert("Don't ask the same question twice!");
    return;
  }
  lastQuestion = document.getElementById("txtQuestion").value;
  ...


}

0
投票

只需创建一个数组,如果问题在数组中,return

var questions = [];

function Response() {
    if (questions.includes(document.getElementById("txtQuestion").value)) {
        alert("You already asked this");
        return;
    } else {
        questions.push(document.getElementById("txtQuestion").value);
    }
    //Rest of your function
}
© www.soinside.com 2019 - 2024. All rights reserved.