# \[SOLVED\] Level : Protect and Serve (JavaScipt)

**URL:** <https://discourse.codecombat.com/t/solved-level-protect-and-serve-javascipt/20978>\
**Category:** Level Help\
**Created:** [April 14, 2020, 3:57am UTC](https://discourse.codecombat.com/t/solved-level-protect-and-serve-javascipt/20978 "2020-04-14T03:57:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [April 14, 2020, 3:57am UTC](https://discourse.codecombat.com/t/solved-level-protect-and-serve-javascipt/20978/1 "2020-04-14T03:57:59Z")

</div>

Due to the lack of hints and countless trial and errors to redo or modify the code i cannot pass this level. The best code i have only makes the soldiers protect the peasants and not the livestock and coins. I would like some help on the matter, if there are any tutorial videos on this subject please send me a link. THX.

---

<div class="post-metadata">

**Author:** ![brotherofall](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brotherofall/32/11594_2.png) [@brotherofall](https://discourse.codecombat.com/u/brotherofall)\
**Post date:** [April 14, 2020, 5:24am UTC](https://discourse.codecombat.com/t/solved-level-protect-and-serve-javascipt/20978/2 "2020-04-14T05:24:39Z")

</div>

@Valentino_Artawan do you still need help? Also can you copy and paste you code.

---

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [April 14, 2020, 10:28am UTC](https://discourse.codecombat.com/t/solved-level-protect-and-serve-javascipt/20978/3 "2020-04-14T10:28:33Z")

</div>

// Protect the workers and animals!

// Defend these two positions:  
var defend = ;  
defend[0] = { x: 98, y: 28 };  
defend[1] = { x: 84, y: 7 };  
defend[2] = { x: 98, y: 67 };  
defend[3] = { x: 74, y: 76 };  
defend[4] = { x: 58, y: 76 };  
defend[5] = { x: 21, y: 67 };  
defend[6] = { x: 22, y: 35 };  
defend[7] = { x: 54, y: 14 };  
var soldiers = ;

var friends = hero.findFriends();  
for(var i=0; i \< friends.length; i++) {  
var friend = friends[i];  
if(friend.type == “soldier”) {  
soldiers.push(friend);  
} else {  
// Defend the workers:  
defend.push(friend);  
}  
}

while(true) {  
// Use a for-loop to assign each soldier to a corresponding defend target  
// Use command(soldier, “defend”, thang) or command(soldier, “defend”, position)  
for(var i=0; i \< friends.length; i++) {  
var friend = friends[i];  
var defense = defend[i];  
if(friend.type == “soldier”) {  
hero.command(friend, “defend”, defend);  
}  
}  
}

I tried to input this. in theory it should pan out (i added those extra defend array numbers) but when i ran it the code didn’t do anything (red x below character)

Please tell me how to solve this problem. I’ve been stuck here for a week  
THX

---

<div class="post-metadata">

**Author:** ![brotherofall](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brotherofall/32/11594_2.png) [@brotherofall](https://discourse.codecombat.com/u/brotherofall)\
**Post date:** [April 14, 2020, 12:47pm UTC](https://discourse.codecombat.com/t/solved-level-protect-and-serve-javascipt/20978/4 "2020-04-14T12:47:46Z")

</div>

@Valentino_Artawan I’m going to break the problem down and try to go step by step to understand better.

Original unedited

```python
// Protect the workers and animals!

// Defend these two positions:
var defend = [];
defend[0] = { x: 98, y: 28 };
defend[1] = { x: 84, y: 7 };

var soldiers = [];

var friends = hero.findFriends();
for(var i=0; i < friends.length; i++) {
    var friend = friends[i];
    if(friend.type == "soldier") {
        soldiers.push(friend);
    } else {
        // Defend the workers:
        defend.push(friend);
    }
}

while(true) {
    // Use a for-loop to assign each soldier to a corresponding defend[] target
    // Use command(soldier, "defend", thang) or command(soldier, "defend", position)

}

```

hints:  
// This level introduces the “defend” command: hero.command(soldier, “defend”, target) the defend target can be another unit, or position. Command soldiers to defend the working peasants! There are equal amounts of peasants and soldiers. Use an array index to iterate over two arrays at once.

---

<div class="post-metadata">

**Author:** ![brotherofall](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brotherofall/32/11594_2.png) [@brotherofall](https://discourse.codecombat.com/u/brotherofall)\
**Post date:** [April 14, 2020, 1:19pm UTC](https://discourse.codecombat.com/t/solved-level-protect-and-serve-javascipt/20978/5 "2020-04-14T13:19:50Z")

</div>

The first part of the problem we want to send the soldiers only to these two positions to defend.

```python
// Defend these two positions:
var defend = [];
defend[0] = { x: 98, y: 28 };
defend[1] = { x: 84, y: 7 };

```

The first part doesn’t require any change.

The second part below:.

```python
var soldiers = []; // empty array we need to add all soldiers to

var friends = hero.findFriends(); // All the friends the hero can see stored in an array
for(var i=0; i < friends.length; i++) { // Loops through each friend in friends and checks if/else statement
    var friend = friends[i]; // To check each friend in the array of friends
    if(friend.type == "soldier") { 
        soldiers.push(friend); // Adds the friend to the soldiers array
    } else {
        // Defend the workers:
        defend.push(friend); // This adds the friend to the defend array so the soldier will go here and defend if same index
    }
}

```

Third part below

```python
while(true) {
    // Use a for-loop to assign each soldier to a corresponding defend[] target

   for (var i = 0; i < soldiers.length; i += 1) {
      var soldier = soldiers[i];
      var something = defend[i]; // This is because it says they are the same amount

    // Use command(soldier, "defend", thang) or command(soldier, "defend", position)

      hero.command(soldier, "defend", ???);
   }
hero.moveXY(100, ??); // Have hero move through coins so the enemy cannot get them
hero.moveXY(??, 77); // top left corner
hero.moveXY(19, ??); // bottom left corner
hero.moveXY(95, ??); // bottom right
}

```

---

<div class="post-metadata">

**Author:** ![brotherofall](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brotherofall/32/11594_2.png) [@brotherofall](https://discourse.codecombat.com/u/brotherofall)\
**Post date:** [April 14, 2020, 1:28pm UTC](https://discourse.codecombat.com/t/solved-level-protect-and-serve-javascipt/20978/6 "2020-04-14T13:28:45Z")

</div>

For the **defend the friends** it uses the friend position because the friend can move around and the soldiers will follow within certain radius. The friend collecting coins at the bottom for example instead of XY. Since we already made _soldiers_ array we can use it as well as the _defend_ array. Also because we made the arrays for **soldiers** && **defend** in the first and second parts it is better to use these in while statement at the end.

---

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [April 14, 2020, 11:44pm UTC](https://discourse.codecombat.com/t/solved-level-protect-and-serve-javascipt/20978/7 "2020-04-14T23:44:31Z")

</div>

So what you’re saying is, the (push) code is used to move an object onto an array?  
i think i understand it now, i’ll input the code now and i’ll tell you the results  
Thanks for the help

---

<div class="post-metadata">

**Author:** ![Valentino\_Artawan](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/valentino_artawan/32/12153_2.png) [@Valentino\_Artawan](https://discourse.codecombat.com/u/Valentino_Artawan)\
**Post date:** [April 14, 2020, 11:50pm UTC](https://discourse.codecombat.com/t/solved-level-protect-and-serve-javascipt/20978/8 "2020-04-14T23:50:14Z")

</div>

It works! thanks for the help

---

<div class="post-metadata">

**Author:** ![brotherofall](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/brotherofall/32/11594_2.png) [@brotherofall](https://discourse.codecombat.com/u/brotherofall)\
**Post date:** [April 15, 2020, 12:03am UTC](https://discourse.codecombat.com/t/solved-level-protect-and-serve-javascipt/20978/9 "2020-04-15T00:03:07Z")

</div>

> [@Valentino\_Artawan](#):
>
> the (push) code is used to move an object onto an array?

Correct the .push method adds to the end of the array.

---

<div class="post-metadata">

**Author:** ![milescombat](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/milescombat/32/34822_2.png) [@milescombat](https://discourse.codecombat.com/u/milescombat)\
**Post date:** [April 1, 2023, 4:35pm UTC](https://discourse.codecombat.com/t/solved-level-protect-and-serve-javascipt/20978/10 "2023-04-01T16:35:34Z")

</div>

I have no idea what to do in the for loop the tell me to use in the bottom
