ユーザーが同じ質問を2回続けて入力すると、アラートボックスが表示されます。だから誰かが「私の日はいいの? 2回連続でアラートボックスが表示されます。彼らが新しい質問をしたら、うまくいくでしょう。彼らは「私の日はいいの?」と尋ねると、新しい答えが再び表示されるはずです。私は数時間試してきましたが、なぜそれが機能していないのか分かりません。同じ質問をするためのJavaScriptアラートボックス
//Create an array for you responses. Remember, it's [0-14].
var responses = [
"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 and ask again."
]
//Create a variable for your user's input or question.
var question;
//Create a variable if user already asked this question.
var alreadyAsked = [];
//
var validQuestion = false;
//Display the output when user.
document.getElementById("submit").onclick = function(){
question = document.getElementsByName("askme")[0].value;
answer = responses[Math.floor(Math.random() * responses.length)];
//If the question has already been asked, display the appropriate alert.
if (alreadyAsked.length >= 1 && alreadyAsked.indexOf(question) == alreadyAsked.length-1){
validQuestion = true;
alert("You've already asked that.");
}
//If the question doe not contain a "?", display the appropriate alert.
else if (question.indexOf("?") == -1){
alert("It appears that you aren't asking me a question.");
}
//If all goes well, then the question will be answered.
else{
if (validQuestion){
alreadyAsked.splice(-1, 1);
}
else{
alreadyAsked.push(question);
}
document.getElementById("answer").innerHTML = answer;
}
};
<div id="container">
<h1>Magic 8 Ball</h1>
<p>What would you like to know?</p>
<input type="text" name="askme" placeholder="Enter a question...">
<br />
<br />
<button id="submit">Ask the 8 Ball</button>
<br />
<br />
<img src="images/8ball.png" alt="https://pixabay.com/p-25774/?no_redirect">
<br />
<h2>The 8 Ball says:</h2>
<p id="answer"></p>
</div>
それは[] alreadyAskedたびに空になる表示されます。だからグローバルにして、価値を保持し、うまくいくはずです! – Kamal