2017-10-03 8 views
1

私は数字が各行で異なっていることを確認する必要があるしかしラインにこのような5回.....リストの乱数を同じ文章で5回印刷するにはどうしたらいいですか?

I just burped 5 times. 
I just burped 5 times. 
I just burped 5 times. 
I just burped 5 times. 
I just burped 5 times. 

を印刷する必要があります。たとえば:

I just burped 5 times. 
I just burped 7 times. 
I just burped 1 times. 
I just burped 9 times. 
I just burped 6 times. 

私は今、この

<h3 style="position: absolute;left:118px;top: 88px;" 
    onClick="myFunction()"><button onclick = "myFunction()"> Click Me </button></h3> 


    <div id="same"></div> 



    <script> 

     function myFunction() { 
      var x = Math.floor((Math.random() * 10) + 1); 
      var text= " "; 
      var i; 
      for(i=0; i<5; i++){ 
       text += "The number of times I just burped is " + x +"<br><br>"; 
      } 
      document.getElementById("same").innerHTML = text ; 
     } 

    </script> 



</body> 

のように見えてきたコード誰もがこれを支援することはできますか?

+1

こんにちは。 StackOverflowへようこそ。 [良い質問をする方法](https://stackoverflow.com/help/how-to-ask)をお読みください。具体的には、結果とは何か、何を期待しているか、どのようなエラーメッセージが表示されたのかなどです。特定の*問題があなたに流れていることを教えてください。 「うまくいきません」というのは、問題を迅速かつ簡単に解決するための十分な詳細ではありません。 – SherylHohman

答えて

3

乱数ジェネレータをforループ内に移動します。

function myFunction() { 
 
    let text = " "; 
 
    for (let i = 0; i < 5; i++) { 
 
    let x = Math.floor((Math.random() * 10) + 1); 
 
    text += "The number of times I just burped is " + x + "<br><br>"; 
 
    } 
 
    document.getElementById("same").innerHTML = text; 
 
}
<h3 style="position: absolute;left:118px;top: 88px;" onClick="myFunction()"><button onclick="myFunction()"> Click Me </button></h3> 
 

 
<div id="same"></div>

+0

ありがとうございました – pblang9

+0

これは、各行で数値が異なることを保証するものではありません。 –

+0

私はOPの問題は、数字が各行で同じであってランダムではないことを前提としていました。しかし、あなたがユニークであることを望むなら、@ guest271314による解決策が働くでしょう。 – H77

0

ループ内のxの割り当てを置きます。そうすれば、それは1行につき変更されます。

<h3 style="position: absolute;left:118px;top: 88px;" 
    onClick="myFunction()"><button onclick = "myFunction()"> Click Me </button></h3> 
    <div id="same"></div> 
    <script> 
     function myFunction() { 
      var text= " "; 
      var i; 
      for(i=0; i<5; i++){ 
         var x = Math.floor((Math.random() * 10) + 1); 

       text += "The number of times I just burped is " + x +"<br><br>"; 
      } 
      document.getElementById("same").innerHTML = text ; 
     }  
    </script> 

Good Luck!

+1

助けてくれてありがとう! – pblang9

+0

https://jsfiddle.net/thedos/7wr7Lts7/が必要な場合は、jsfiddleは問題ありません。 –

1

あなたは、配列を作成し、配列をコピーする.slice()を使用して.splice()は、以前に配列

<h3 style="position: absolute;left:118px;top: 88px;" onClick="myFunction()"><button onclick="myFunction()"> Click Me </button></h3> 
 
<div id="same"></div> 
 
<script> 
 
    function myFunction() { 
 
    var x = [0,1,2,3,4,5,6,7,8,9]; 
 
    var text = " "; 
 
    var i; 
 
    for (i = 0, copy = x.slice(); i < 5; i++) { 
 
     text += "The number of times I just burped is " 
 
       + copy.splice(Math.floor(Math.random() * copy.length), 1)[0] 
 
       + "<br><br>"; 
 
    } 
 
    document.getElementById("same").innerHTML = text; 
 
    delete copy; 
 
    } 
 
</script>
から削除番号の重複はない結果内の数を設定するには、配列から要素を削除することができます

関連する問題