2017-12-03 16 views
0

私はデジタルアーツの学生です。私は初年です。最近、数学のための基本的なEラーニングウェブサイトを作成する作業が行われました。基本的には、例えば7回7秒間、回答を入力してボタンをクリックすると、警告ウィンドウが表示されます。もしあなたが正しいのであれば、それは "正しい"、 "いいえ"、あるいは "うまくやった"と言われ、間違っているなら "いいえ"や "やり直す"などと言います。window.alert(Javascript)でランダムメッセージを割り当てます。

私の問題は、ウィンドウアラートに1つのランダムなメッセージを表示することです。すべてのメッセージを同時に表示するのではなく、1つずつランダムに表示することになっています。私は見ましたが、私は何かを助けるものを見つけることができませんでした。ここで

は、私はとのトラブルを抱えているコードの一部です:

<script type="text/javascript"> 
    var x, y; 
    var answer; 



    function aNumber() { 

     return Math.floor(1 + (Math.random() * 12)); 

    } 

    function genQuestion() { 
     x = aNumber(); 
     y = aNumber(); 
     din = document.getElementById("inputVal"); 
     din.value = x + " times " + y; 
     answer = x * y; 
    } 



    function ClickAnswer() { 
     if (answer == document.getElementById("outputVal").value) { 
      window.alert("Correct"); 
      window.alert("Yes!"); 
      window.alert("Well done"); 
      location.reload(); 

     } else { 
      window.alert("No, try again."); 
      window.alert("try again"); 
      window.alert("wrong"); 
     } 

    } 

    function displayArea() { 
     din = document.getElementById("inputVal"); 
     dout = document.getElementById("outputVal"); 
     dout.value = circleArea(din.value); 
    } 

</script> 

答えて

0

問題は、あなたがこのように同時にすべてのメッセージを書いているということです。

window.alert("Correct"); 
    window.alert("Yes!"); 
    window.alert("Well done"); 

代わりにあなたがメッセージを配列に格納し、乱数を取り出してこの配列からメッセージを選択することができます。

var message = ["Correct","Yes!","Well done"]; 
 

 
var a = Math.floor(Math.random() * message.length); 
 

 
window.alert(message[a]);

+0

である可能性があります。使用する必要がありますmessage.length – webdevdani

+0

@ webdevdani確かに、私はそれを変更..私は行う方法に焦点を当てていた;) –

3

配列にメッセージを入れて、それをランダムにエントリを警告:あなたはこのような何かをしようとします。

function ClickAnswer() { 
    if (answer == document.getElementById("outputVal").value) { 
     alert(successMsg[Math.floor(Math.random() * successMsg.length)]); 

     location.reload(); 

    } else { 
     alert(errorMsg[Math.floor(Math.random() * successMsg.length)]); 
    } 
0

let successMsg = ['Correct', 'Cool']; 
 
let errorMsg = ['Wrong', 'false']; 
 

 
alert(successMsg[Math.floor(Math.random() * successMsg.length)]);
は何をする探していると、あなたは番号の選択のために行ったよう Math.random()を利用することです。 1つの配列に正しい答えをそれぞれ割り当て、別の配列に間違った答えをそれぞれ割り当てます。そして、あなたがそれらを取得することができます。

correct_answers[Math.floor(Math.random()*correct_answers.length)]; 
imcorrect_answers[Math.floor(Math.random()*incorrect_answers.length)]; 

私はあなたの元のコードの機能縮小版を作成しました。このここに展示:

var correct_answers = ["Correct", "Yes!", "Well done"]; 
 
var incorrecty_answers = ["No, try again.", "try again", "wrong"]; 
 

 
correct_answers[Math.floor(Math.random()*correct_answers.length)]; 
 

 
function ClickAnswer() { 
 
    if (true) { 
 
    window.alert(correct_answers[Math.floor(Math.random()*correct_answers.length)]); 
 
    } else { 
 
    window.alert(incorrect_answers[Math.floor(Math.random()*incorrect_answers.length)]); 
 
    } 
 
} 
 

 
ClickAnswer();

ホープ、このことができます! :)

0

あなたが行くことができる1つの方法は、配列にランダム化したい文字列値を格納し、その中から特定のインデックスをランダムに選択して返します。見てくださいhere。あなたのケースでは

それは長さが非常に管理していないハードコーディング

var positiveAnswers= ['Correct!', 'Yes!', 'Well done!'];  
var randomAnswer = positiveAnswers[Math.floor(Math.random() * positiveAnswers.length)]; 

window.alert(randomAswer); 
関連する問題