PS2 Xbox Nintendo Dreamcast Games - Debt Consolidation - Free Advertising - Discount Perfume - Internet Advertising

PDA

View Full Version : Please Help with this array


JEET
Oct 23rd 2008, 11:18 am
Hi,
I have 2 arrays

ww= (401, 402, 403, 404);
cc= (399, 400, 401);

I need to find if there's a matching element in both arrays. Ex: 401 is matching above.

I can do this by looping in both arrays, but that's slowing the script a lot. (this is inside another big loop)
Is there a fast way of checking this?

Kindly help
Thanks :)

lp1051
Oct 23rd 2008, 1:15 pm
Hi, I don't think you can do it without looping, anyway... You can search for 'javascript array_diff' if you want ready made function.
Don't know about speed, but other option would be to convert each array into object, where the value of array will become the member of object, but that doesn't accept numbers, so you would need to add some non-decimal char e.g. '_401':0. Then you can use IN operator.
Hard to say what's better, as I don't know much about the context...

MMJ
Oct 23rd 2008, 1:42 pm
I'm pretty sure you can only do this by looping but it may be possible to change your logic so that you use a different method.

What exactly are you doing?

JEET
Oct 24th 2008, 1:36 pm
Hi,
I am trying to find the coinciding points of 2 boxes (rectangles)
These are 2 divs on the page and I need to know if they are "overlapping" each other (even touching) or not. If they are, the script shows an alert() message.
Any way to detect this quickly will be best. Loops are slowing the script too much.
I know the points that make the borders of these 2 divs... However if I could only use the four coordinates to do the check, it'll speed things a lot (not sure about accuracy).

Thanks :)

cont911
Oct 24th 2008, 9:25 pm
Hope this help
<script>
//x11, x12 - x coordinates, y11, y12 - y coordinates for first div
//x21, x22 - x coordinates, y21, y22 - y coordinates for second div

function overlapping(x11, x12, x21, x22)
{
return Math.max(x11, x21) < Math.min(x12, x22);
}

function rectanglesOverlap(x11, x12, x21, x22, y11, y12, y21, y22)
{
return overlapping(x11, x12, x21, x22)
&& overlapping(y11, y12, y21, y22);
}

x11 = 120;
x12 = 220;

x21 = 150;
x22 = 210;

y11 = 340;
y12 = 450;

y21 = 120;
y22 = 420;


alert(rectanglesOverlap(x11, x12, x21, x22, y11, y12, y21, y22));

</script>

JEET
Oct 25th 2008, 9:00 am
Hi,
Thanks for these functions. I'll try these. Green points added. :)