# HOw to find middle of array

• Jul 21, 2011, 03:40
whteout
HOw to find middle of array
is this correct

var mid = math.floor((0 + array.length)/2)

from here you could use array.slice to divide the array into two equal sized arrays?
• Jul 21, 2011, 03:53
paul_wilkins
Quote:

Originally Posted by whteout
is this correct

var mid = math.floor((0 + array.length)/2)

from here you could use array.slice to divide the array into two equal sized arrays?

That would be possible if you have an even length array, but dividing an odd-sized array in to two equal sized arrays could be a challenge.
• Jul 21, 2011, 03:53
guido2004
Did you try?

You can divide the array in two equal sized arrays if the array contains an even number of elements. If it's uneven, one part will have one element more than the other part.
• Jul 21, 2011, 03:54
guido2004
Quote:

Originally Posted by paul_wilkins
That would be possible if you have an even length array, but dividing an odd-sized array in to two equal sized arrays could be a challenge.

You just beat me to it :D
• Jul 21, 2011, 03:55
paul_wilkins
Quote:

Originally Posted by guido2004
You just beat me to it :D

Great minds, eh?
• Jul 21, 2011, 19:46
whteout
OK so now that I have the middle figured, how can I split the array into two arrays?
• Jul 21, 2011, 20:02
paul_wilkins
Quote:

Originally Posted by whteout
OK so now that I have the middle figured, how can I split the array into two arrays?

I'd say that the .splice() method would be the best way to do that.