显示繁体中文内容    显示简体中文内容

javascript - How to get a for loop create a string of 9 random 1digit number?
javascript - 如何获取for循环创建一个9个随机1digit数字的字符串?

109 5

I need to create a random number generator with 9 total digit created from a random 1 digit number generated thru a for loop.

This one works but i need to use a for loop for it :


 var random1 = Math.floor(Math.random() * 9) + 1 ;


 var random2 = Math.floor(Math.random() * 9) + 1 ;


 var random3 = Math.floor(Math.random() * 9) + 1 ;


 var random4 = Math.floor(Math.random() * 9) + 1 ;


 var random5 = Math.floor(Math.random() * 9) + 1 ;


 var random6 = Math.floor(Math.random() * 9) + 1 ;


 var random7 = Math.floor(Math.random() * 9) + 1 ;


 var random8 = Math.floor(Math.random() * 9) + 1 ;


 var random9 = Math.floor(Math.random() * 9) + 1 ;



//... and then dump the random number into our random-number div.


 $("#random-number").text(""+ random1 + random2 + random3 + random4 + random5 + random6 + random7 + random8 + random9);



 var results =""+ random1 + random2 + random3 + random4 + random5 + random6 + random7 + random8 + random9 ;


 $("#results").prepend(results +" <br>");



the above code works in creating a random 9 digit number but i need to use a for loop to make my code concise.

时间:

113 1

You can use for loop.Create a string and in each loop concentrate the new random number with that string.


let random = '';


for(let i =0;i<9;i++){


 random += Math.floor(Math.random() * 9) + 1 ;


}


$("#random-number").text(random);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div id="random-number"></div>

Another way can be using map() and join()


let random = [...Array(9)].map(x => Math.floor(Math.random() * 9) + 1).join('')


$("#random-number").text(random);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div id="random-number"></div>
原作者:
103 3

You can try this way with all digits is in range [0, 9 ] and you should relize that first digit can be zero :


var digits = [];


for (let i=0; i<9; i++) {


 let n = Math.floor(Math.random()*10);


 digits.push( n );


}


var number = digits.join('')



103 1

Construct a new array where the items are random numbers and then join the array with an empty string.


const random = length => Array.from(Array(length), _ => Math.floor(Math.random() * 9) + 1).join('');



console.log(random(9));


console.log(random(9));


console.log(random(9));


console.log(random(9));


console.log(random(9));
100 0

In case you want to have all the 9 number to be unique you can use a object to keep track of added previously added numbers and if it is not included add a it to random number as well as object


let random = '';


let included = {}


for(let i =0;i<9;i++){


 let temp = true


 while(temp){


 let num = Math.floor(Math.random() * 9) + 1 ;


 if(included[num] === undefined){


 temp = false


 random += num


 included[num] = num


 }


 }


}


$("#random-number").text(random);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div id="random-number"></div>
原作者:
...