Home > front end >  Array to join member, must reach to the condition of problem
Array to join member, must reach to the condition of problem

Time:02-05

Ask you a favor, thought for a long time all can't think of, here thank you for your first

CodePudding user response:

Requests for the final number of elements in the array without circulation 50 times no problem, have the requirement is to determine the length of the array
 
Var arr=[];
for(var i=0; i<50; I++) {
Var n=parseInt (1 + Math. The random () * 60);
If (arr. Length==0 | | math.h abs (n - arr (arr. Length - 1)) & gt; {
=3)Arr. Push (n);
}
}
The console. The log (arr);

CodePudding user response:

Hello World, 1/f, reference response:
no requirement to the final number of elements in the array cycle 50 times no problem, if you have any requirements to determine the length of the array
 
Var arr=[];
for(var i=0; i<50; I++) {
Var n=parseInt (1 + Math. The random () * 60);
If (arr. Length==0 | | math.h abs (n - arr (arr. Length - 1)) & gt; {
=3)Arr. Push (n);
}
}
The console. The log (arr);


(it's even) wow, great, so he can, thank you prawns

CodePudding user response:

Hello World, 1/f, reference response:
no requirement to the final number of elements in the array cycle 50 times no problem, if you have any requirements to determine the length of the array
 
Var arr=[];
for(var i=0; i<50; I++) {
Var n=parseInt (1 + Math. The random () * 60);
If (arr. Length==0 | | math.h abs (n - arr (arr. Length - 1)) & gt; {
=3)Arr. Push (n);
}
}
The console. The log (arr);


Can you help to add one more condition? There can be no repeat number,

CodePudding user response:

 
Var arr=[], dic={};
for(var i=0; i<50; I++) {
Var n=parseInt (1 + Math. The random () * 60);
If (arr. Length==0 | | (dic [n]==undefined & amp; & Math. Abs (n - arr/arr. Length - 1) & gt; {
=3))Arr. Push (n);
Dic [n]=0;
}
}
The console. The log (arr);

CodePudding user response:

The
Hello World reference 4 floor, response:
 
Var arr=[], dic={};
for(var i=0; i<50; I++) {
Var n=parseInt (1 + Math. The random () * 60);
If (arr. Length==0 | | (dic [n]==undefined & amp; & Math. Abs (n - arr/arr. Length - 1) & gt; {
=3))Arr. Push (n);
Dic [n]=0;
}
}
The console. The log (arr);


I found there are wrong, the first floor there, 24 and 25 apart is not greater than or equal to 3, a member of the new and old members individually, must & gt;=3 to add, just didn't notice that...
  • Related