2017-09-23 9 views
0

ユーザーが同じ質問を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>

+0

それは[] alreadyAskedたびに空になる表示されます。だからグローバルにして、価値を保持し、うまくいくはずです! – Kamal

答えて

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>
を使用して、最後に保存した質問で現在の質問を比較することができます

0

ここで何をする必要があります。

  1. 質問をinputから取得します。
  2. 質問には? sign.Showアラートを表示しない場合。
  3. 前の質問と同じ質問であるかどうかを確認します。
  4. 質問が有効で以前と同じでない場合は、この質問を前回の質問と同様に設定してください(次の質問を確認できるように)、回答を入力してください。

これを試してみてください:

<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> 

関連する問題