Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

I need help with understanding "for" loops (JavaScript) [SOLVED]

im currently on computer science 4 level 35 “HUNTING PARTY” and i need help understanding the For loop that i need to put in the code, can someone explain how they work? thanks

Please post your code so we can help you

while (true) {
var friends = hero.findFriends();
---->

    var enemy = hero.findEnemies();
    if(enemy){
        hero.command(friends, "attack", enemy);
    }
    hero.command(friend, "move", {'x': friend.pos.x + 15,'y': friend.pos.y});

}
}

i just need help understanding how they work. however i just posted my code for you to see

and sorry if its not formatted properly, im new to codecombat discourse.

If you are referring to how loops work, they basically repeat your code in the same order that it is written in.

i understand how loops work, however “For” loops are confusing me with there requirements to allow them to work.

1 Like

Go to https://goo.gl/wEwBV6 :


Play with Prev, Back, Forward & Last buttons. Observe variables on the right.

1 Like

@Alexander_Hollowell, basically a for loop is a loop that is controlled by different words.
You would write it in java something like this

public class JavaForLoop {

     public static void main(String[] args) {
         //print integers 5 to 10
         for (int i=5; i<=10; i++) {
             System.out.println("Java for loop example - " + i);
           }
    }
}

Now I know my brackets aren’t perfect, but this is an example of a for loop.

Check out this website if you would like to read more about for loops.

:heart: this post if it helps you. :heart: this post if the website is helpful :joy: :smile: :smile: :joy_cat: :beers: