Ask bosses give a train of thought, or any other method to achieve
//get element
Var front=document. GetElementById (' front ');
Var spans=front. The children;
//in 16 randomly generated 1-8 digital span, twice in every number
//used to store 1 ~ 8 8 random number;
Var arr1=[];
//used to store 0 ~ 15 to 16 random number;
var arr2=[];
For (var I=0; I & lt; Spans. Length/2; I++) {//here if I & lt; Span length, will lead to find when the ninth cycle () function conflict
Var random1=getRandom1 ();
Var random2=getRandom2 ();
//determine whether random number 1-8 in an array, not seen just put random Numbers in the arr1;
if (! Find (random1, arr1)) {
Arr1. Push (random1);
//determine whether random number 0 to 15 in the array, hadn't been put random Numbers in the arr2;
if (! Find (random2 arr2)) {
Arr2. Push (random2);
Spans [random2] innerHTML=random1;//will be randomly generated in 16 span number, but random2 randomly to repeat Numbers, there are Numbers of span of less than eight
}
} else {
I -;//repeat it returns recycle
}
//at this point in the only part of the span number
}
The console. The log (arr1);
The console. The log (arr2);
The console. Dir (spans);
//to find the item in the arr is seen, seen returns true, have not seen or false;
The function find (item, arr) {
For (var I=0; I & lt; Arr. Length; I++) {
If (item===arr [I]) return true;
}
return false;
}
//generated random Numbers 1-8
The function getRandom1 (random) {
Var random=Math. Ceil ((Math. The random () * spans in length/2));
Return the random;
}
//to generate random Numbers 0-15, for span box
The function getRandom2 (random) {
Var random=Math. Floor ((Math. The random () * spans in length));
Return the random;
}
CodePudding user response:
Var arr=[];
For (var I=1; I & lt;=spans. Length/2; I++) {
Arr. Push (I, I);
}
For (var I=0; I & lt; Spans. Length; I++) {
Spans [I] innerHTML=arr. Splice (Math) floor ((Math) random () * arr in length)), 1) [0].
}
CodePudding user response: