Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

How to find number of enemies in an array


Hey, I’m having trouble with the desert brawl. I need to know what javascript code I would use to find how many enemies are in the array of detected enemies, so that, if, within a distance of 5 meters, there are >2 enemies, and if cleave is ready, then cleave them, else, continue;. Plz put it in javascript! Thanks!


You could try array.length to get the length of the array.

For example:

for(var i=0; i< enemyArray.length; i++){
    // check distance to enemyArray[i]


hmmm, that doesn’t really help. I have no idea what any of those commands mean, except for the for command, but i don’t even know how to use the for command! May you please just put everything I said in my first message into an if statement? Thanks!


There isn’t any way to put all the things you wanted into a single if statement. Here’s how to go about doing it:

First create a while – loop that loops over all enemies. If an enemy is less then 5 m away, then append it to an array(Using push, I believe?). Then, use an if – statement to check if array.length is greater than two. if so, check 'cleave` is ready. If so, cleave.