Home > front end >  How to put in random 16 containers (1 ~ 8) 16 Numbers, for 1 ~ 8 each number two
How to put in random 16 containers (1 ~ 8) 16 Numbers, for 1 ~ 8 each number two

Time:11-28

code can only be random gives part of the span number (and not more than eight span), how can ability let all span have digital
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:

reference 1/f, the sky wave 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].
}


Arr. Splice (Math. Floor ((math.h random () * arr in length)), 1)
This is random delete an array of digital
But what is the role of [0] behind ah, is it {arr. Splice (math.h floor ((math.h random () * arr in length)), 1)} the deleted content will also be an array, and then take the first?

CodePudding user response:

Is it 1 ~ 8 random take 2 times, in the 16 grid?

CodePudding user response:

bubble fish _ reference 3 floor response:
is 1 ~ 8 random take 2 times, in the 16 grid?

Yes, the requirement is 16 in the grid, 1-8 each two, random distribution

CodePudding user response:

  lazy way,
 
Var arr=[1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8].
Arr. Sort ((a, b)=& gt; Math. The random () 0.5);
console.log(arr);

CodePudding user response:

Splice can remove multiple elements of the array, and returns an array composed of multiple elements were removed,
If only remove one element is returned only one element of array,
  • Related