Comparing values within an array to each other
<p>I am new to JavaScript and HTML and am working on a small game.  </p>

<p>I have four <strong>'enemies'</strong> whose position on the canvas is determined by the values in the arrays <strong>'enemyX'</strong> and <strong>'enemyY'</strong>.  </p>

<p>Very simply, I want to detect if the enemies have <strong>'collided'</strong>, i.e have moved within <code>30px</code> of each other (the enemy image is 30px by 30px).  </p>

<p>What I want to do is subtract the value of the <strong><em>i</em></strong> th value in the array with the other values in the <em>same array</em> and see if this value is less than <strong>30</strong>. The less than <strong>30</strong> part is an if statement, so how do I go about subtracting all the values from each other without many lines of code?   </p>

<p>Here's what I have tried based on the answers below:</p>

<pre><code>    var count = 0;
var innercount = 0;

while (count &lt;= 3) {

    while (innercount&lt;=3) {
        collisionDetect(count, innercount, enemyX[count], enemyY[count], enemyX[innercount], enemyY[innercount])
        innercount++
    }
    count++    
}

var i = 0;
while (i &lt; enemyX.length) {

    if (collisionX[i] == 1) {
        directionX  = directionX*-1;
    }

    if (collisionY[i] == 1) {
        direction = directionY*-1;
    }
}


}



}

function   collisionDetect(count, innercount, x, y, xX, yY ) {


if ((Math.abs(x-xX)) &lt;=30) {
    collisionX[count] = 1
    collisionX[innercount] = 1
}

if ((Math.abs(y - yY)) &lt;=30) {
    collisionY[count] = 1
    collisionY[innercount] = 1
}

return collisionX, collisionY;
}
</code></pre>

<p>This code gives me a blank canvas.</p>
