Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Multiple Arrays

I have a quick question with multiple array and random numbers. If i generate my random numbers in one array, how would i take a selection of those numbers and put them in another array?

Ex: array 1: 25, 34, 38, 40, 22, 49

want to move numbers between 30 and 50 to another array.

array 2: 34, 38, 40, 49

is it as simple as for loops and if statements setting the conditions?
do i use a sorting method? (selection? bubble?)

any help would be appreciated.

• Do you *WANT* them sorted in the other array?

Or do you want them in the same order?

There are a ton of ways to do this.

• well they dont have to be sorted from low to high they just have to be sorted into the specific array.
just if the numbers fall between this range, then they go in this array.

example to help clarify..
array 1:1-10
array 2: 11-20
array 3 21-30

array 4(random): 5, 12, 3, 2, 29,4,7

The numbers to be organized are from the random array

• Ahhh...you have changed the question, so what I was thinking doesn't even matter.

I thought you had only *ONE* destination array.

If you have multiple destinations, then just the brute force approach will be best.
Code:
```var source = [5, 12, 3, 2, 29,4,7];
var to10 = [];
var to20 = [];
var to30 = [];
var temp = [to10, to20, to30];

for ( var i = 0; i < source.length; ++i )
{
var n = source[i];
var decade = Math.floor( (n-1) / 10 );
}```

• a sufficiently modern browser can also use the filter method.
PHP Code:
``` var to10 = source.filter(function (el) { return (el >  0 && el < 11); });var to20 = source.filter(function (el) { return (el > 10 && el < 21); });var to30 = source.filter(function (el) { return (el > 20 && el < 31); });  ```

• alright so your using if with a && operator.

I'll get to work and post my results later!

Thanks

• Yeah, unless this is homework, and unless it has to work on older browsers, use Dormilich's answer.

If it's homework, likely the prof won't believe you wrote either answer.

• Originally Posted by Dormilich
a sufficiently modern browser can also use the filter method.
taking advantage of late-bound this can simplify and shorten:
Code:
```function compare(el) { return el > this[0]  &&  el < this[1] ; }
var to10 = source.filter(compare, [ 0,11]);
var to20 = source.filter(compare, [10,21]);
var to30 = source.filter(compare, [20,31]);```
this way, you can create more array slots without coding new filter functions for each range...

• Oh, w.t.h. Wasn't going to do this, but...

Since we are all getting in on the act...

One advantage of the brute force method is that it can be self adapting.
Code:
```<script type="text/javascript">
var source = [5, 12, 3, 2, 29,4,7, 48, 41, 57, 22, 1, 4.3, 17.999 ]; // make this as big as you want, with values up to hundreds

for ( var i = 0; i < source.length; ++i )
{
var n = source[i];
var decade = Math.floor( (n-1) / 10 );
}

for ( var d = 0; d < decades.length; ++d )
{
if ( decades[d] != null )
{
}
}
</script>```
Only limitation is that the numbers must be 1 or greater, because of how he defined the ranges.

• Originally Posted by Old Pedant
Oh, w.t.h. Wasn't going to do this, but...

Since we are all getting in on the act...

One advantage of the brute force method is that it can be self adapting.
...
Only limitation is that the numbers must be 1 or greater, because of how he defined the ranges.

good ideas.
functional can also be self-adapting, and using an object permits negative decades:

Code:
```var source = [
5, 12, 3, 2, 29,4,7, 48, 41, 57, 22, 1, 4.3, 17,323, -6,-13,-18.4,-55,-75,-434 ];

var bns= a>0? 0 : 1,
i= bns+Math.floor(a/10) || (bns? "-0" : 0),
r= this[i] || (this[i]= []);
r[r.length]= a;
return this;
},{})[0];

• That puts 0 into the "1" decade and puts all multiples of 10 into the wrong decade. Latter is easily fixed. I'm not clear why ends up in "1".

• Originally Posted by Old Pedant
That puts 0 into the "1" decade and puts all multiples of 10 into the wrong decade. Latter is easily fixed. I'm not clear why ends up in "1".
DOH!

that code only has problems with negative multiples of 10, didn't notice in my test case.

fixes in green:
Code:
```var source = [
5, 12, 3, 2, 29,4,7, 48, 20,30,40,0,41, 57,60,64,-60,-64, 22, 1, 4.3, 17,323, -6,-13,-18.4,-55,-80,-75,-434 ];

var bns= a>=0? 0 : 1,
i= bns+Math.floor((a/10)-negPad) || (bns? "-0" : 0),
r= this[i] || (this[i]= []);
r[r.length]= a;
return this;
},{})[0];

• Originally Posted by Old Pedant
Oh, w.t.h. Wasn't going to do this, but...

Since we are all getting in on the act...

One advantage of the brute force method is that it can be self adapting.
Two improvements:-

var source = [5, 12, 3, 2, 29,4,7, 48, 41, 57, 22, 1, 4.3, 17.999 ];
source.sort(function(a,b){return a - b});

for ( var d = 0; d < decades.length; ++d ){
if ( decades[d] != null ) {
document.write( "<hr>decade " + d + ":&nbsp&nbsp&nbsp " + list);

}

• Question...

if i document.write the array, shouldnt it show the newly indexed numbers?

in my code, im just getting comas and it seems to be indexing the appropriate amount of commas for the random numbers but its just not showing it.

• Whose code are you trying to use? Old Pedant's or rnd me's?
I have to say that they both work just fine for me.

•
Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•