我试图在用户连续两次输入相同问题时设置一个警告框,它会显示一个警告框。所以,当有人问,"我的一天会好吗?"连续两次,将显示一个警告框。如果他们提出新问题,那就没问题了。当他们回去询问"我的日子会好吗?",它应该再次显示一个新的答案。我现在已经尝试了几个小时而且我不确定它为什么不起作用。
//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>
&#13;
答案 0 :(得分:0)
你可以继续按照你想要的方式将问题推到数组中,但仅将当前问题与上次保存的问题进行比较
alreadyAsked[alreadyAsked.length-1] === question
<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>
<script>
//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[alreadyAsked.length-1] === question){
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{
alreadyAsked.push(question);
document.getElementById("answer").innerHTML = answer;
}
};
</script>
</div>
&#13;
答案 1 :(得分:0)
这是你需要做的。
input
。试试这个:
<html>
<body>
<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>
<script>
//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 to store previous question
var previousQuestion = null;
//Display the output when user.
document.getElementById("submit").onclick = function() {
question = document.getElementsByName("askme")[0].value;
//If the question doe not contain a "?", display the appropriate alert.
if (question.indexOf("?") == -1) {
alert("It appears that you aren't asking me a question.");
}
//If the question has already been asked, display the appropriate alert.
else if (previousQuestion && previousQuestion === question) {
alert("You've already asked that.");
}
//If all goes well, then the question will be answered.
// Set this question as previous question to compare with next time.
else {
previousQuestion = question;
answer = responses[Math.floor(Math.random() * responses.length)];
document.getElementById("answer").innerHTML = answer;
}
};
</script>
</div>