2017-09-29 6 views
-2

されていない場合、私はマジック8ボールをやって、次のように表示する必要があるんだエラーボックスが必要になります。質問は「doesnの場合、同じ質問が2回連続尋ねた、またはされている場合は何クエスチョンマークが入力

最後に "?"を付けて、アラート機能を使って適切なエラーメッセージを出してください。

<!DOCTYPE html> 
 

 
<body style="background-color:black"> 
 

 
    <font size=+ 4 color="white">Magic 8 Ball</font> 
 

 
    <script> 
 
    <!-- Begin 
 
    var answers = new Array(
 
     "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", 
 
    ); 
 

 
    function fortune() { 
 
     num = Math.round((answers.length - 1) * Math.random()); 
 
     return answers[num]; 
 
    } 
 
    // End --> 
 

 
    </script> 
 

 
    <form> 
 
    <b style="color:white">What would you like to know?</b> 
 
    <br> 
 
    <br> 
 
    <br> 
 

 
    <input type=text name=question size=35> 
 

 
    <input type=button name=ask value="Ask the 8 ball" onClick="if 
 
(this.form.question.value!='') this.form.answer.value = fortune();"><b style="color:white"> 
 

 
<br>The 8 ball says:</b> 
 
    <br> 
 

 
    <br> 
 
    <input type=text name=answer size=35> 
 
    </form>

+1

あなたはすでに書いたコードを投稿することができますか? –

+0

提供された情報を手助けすることはできません。具体的に何が問題になっていますか?現在のコードと明示的な質問を投稿してください。 –

+0

コードを提供してもらえますか? –

答えて

0

ストア前の質問変数で、各ターンに比較します。以下のようなもの。あなたのニーズに合わせてコードを変更してください。ちょうどあなたのアイデアが得られます。

<!DOCTYPE html> 
 

 
<body style="background-color:black"> 
 

 
    <font size=+ 4 color="white">Magic 8 Ball</font> 
 

 
    <script> 
 
    <!-- Begin 
 
    var previousQuestion = null; 
 
    var answers = new Array(
 
     "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", 
 
    ); 
 
    
 
    //make this function take a string parameter that will be the users submitted question 
 
    function fortune(question) { 
 
     var validQuestion = true; 
 
     //check if the string meets our criteria 
 
     if (question === previousQuestion || question.slice(-1) != "?") { 
 
     validQuestion = false; 
 
     } 
 
     //if string is invalid, no need to continue 
 
     if (!validQuestion) { 
 
     //give user error alert 
 
     alert("Ask a new question or include question mark"); 
 
     //"reset" the answer textbox 
 
     return ""; 
 
     } 
 
     //since we know question is valid at this point, store the question for next go-around 
 
     previousQuestion = question; 
 

 
     num = Math.round((answers.length - 1) * Math.random()); 
 

 
     return answers[num]; 
 
    } 
 
    // End --> 
 
    </script> 
 

 
    <form> 
 
    <b style="color:white">What would you like to know?</b> 
 
    <br> 
 
    <br> 
 
    <br> 
 

 
    <input type=text name=question size=35> 
 

 
    <input type=button name=ask value="Ask the 8 ball" onClick="if 
 
(this.form.question.value!='') this.form.answer.value = fortune(this.form.question.value);"><b style="color:white"> 
 

 
<br>The 8 ball says:</b> 
 
    <br> 
 

 
    <br> 
 
    <input type=text name=answer size=35> 
 
    </form>

+0

ありがとう!!!!!! – Ashley

+0

問題ありません。私は各ステップで何をしているのかを示すコメントを追加しました。また、これがあなたの質問に答えた場合は、それを受け入れられた回答としてマークしてください。 – tommyO

関連する問題