如何防止用户两次问相同的问题?

时间:2019-02-24 23:59:19

标签: javascript

如何防止用户两次问相同的问题?我已经有了,所以他们必须以问号结束他们的问题。只是需要防止他们连续两次问相同的问题,而不必永久存在。

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>

4 个答案:

答案 0 :(得分: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!");
    }

}

答案 1 :(得分:1)

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

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?

答案 2 :(得分: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;
  ...


}

答案 3 :(得分: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
}
相关问题