2

How would I prevent the user from asking the same question twice? I already have it so they have to end their question with a question mark. It just needs to prevent them from asking the same question twice in a row doesn't have to permanent.

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>
zmag
  • 7,825
  • 12
  • 32
  • 42
  • 1
    Am I right to assume that you don't want them to ask the same question because your randomiser will produce a different answer each time? – Matt Way Feb 25 '19 at 00:03
  • you could save the question when the user sends it, and then when they send the next one, compare with the saved one to see if it's the same. it doesn't sound very difficult. what problem are you facing? – JV Lobo Feb 25 '19 at 00:04

4 Answers4

1

Why not keep all the questions?

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!");
    }

}
see sharper
  • 11,505
  • 8
  • 46
  • 65
1

This is quite a deep question. How do you define a question to be the same? Naively, you could simply store a hash of the exact question text, and the random choice, so that if they do ask the same exact question again, they will get the same answer. For example:

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>

However, what if someone asked 'Am I going to die tomorrow?' and 'Will I die tomorrow?'

These are arguably the same question, and would require a much more sophistocated solution, for which you would need to utilise natural language processing. It's an enormous field with lots of surrounding questions. For example: How to detect that two sentences are similar?

Matt Way
  • 32,319
  • 10
  • 79
  • 85
0

Save last question in global variable and compare with current one.

var lastQuestion = ''

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


}
zmag
  • 7,825
  • 12
  • 32
  • 42
0

Just make an array, and if the question is in the array, 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
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79