# Noble sacrifice help JS

**URL:** <https://discourse.codecombat.com/t/noble-sacrifice-help-js/5860>\
**Category:** Uncategorized\
**Created:** [December 2, 2015, 11:57pm UTC](https://discourse.codecombat.com/t/noble-sacrifice-help-js/5860 "2015-12-02T23:57:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pizza\_Banana](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Pizza\_Banana](https://discourse.codecombat.com/u/Pizza_Banana)\
**Post date:** [December 2, 2015, 11:57pm UTC](https://discourse.codecombat.com/t/noble-sacrifice-help-js/5860/1 "2015-12-02T23:57:46Z")

</div>

// Collect 80 gold  
var item = this.findNearest(this.findItems());  
if (item && this.gold \<= 80) {  
this.moveXY(item.pos.x, item.pos.y);  
}  
// Build 4 soldiers to use as bait  
if (this.gold \> 4\*this.costOf(“soldier”)) {  
this.summon(“soldier”);  
this.summon(“soldier”);  
this.summon(“soldier”);  
this.summon(“soldier”);  
}  
// Send your soldiers into position  
var points = [];  
points[0] = { x: 13, y: 73 };  
points[1] = { x: 51, y: 73 };  
points[2] = { x: 51, y: 53 };  
points[3] = { x: 90, y: 52 };  
var friends = this.findFriends();  
var friendIndex = 0;  
var friend = friends[friendIndex];  
var point = points[i];

// Use a for-loop to loop over i from 0 to 4  
// Match the friends to the points and command them to move  
for (var i = 3; i \> -1; i–) {  
this.command(friend, “move”, point);  
friendIndex ++;  
}  
I used this code but I could not pass the level. It says hero placeholder needs something to command on the command line. Please Help!!!

---

<div class="post-metadata">

**Author:** ![Hinkle](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hinkle/32/3967_2.png) [@Hinkle](https://discourse.codecombat.com/u/Hinkle)\
**Post date:** [December 3, 2015, 1:27am UTC](https://discourse.codecombat.com/t/noble-sacrifice-help-js/5860/2 "2015-12-03T01:27:46Z")

</div>

That error message usually means you are trying to command something that doesn’t exist. Did you remember to check for friend before you commanded? The command code may have executed before you had any friends to command.

---

<div class="post-metadata">

**Author:** ![Pizza\_Banana](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Pizza\_Banana](https://discourse.codecombat.com/u/Pizza_Banana)\
**Post date:** [December 3, 2015, 6:28am UTC](https://discourse.codecombat.com/t/noble-sacrifice-help-js/5860/3 "2015-12-03T06:28:35Z")

</div>

I get it now! Thanks

---

<div class="post-metadata">

**Author:** ![Pizza\_Banana](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Pizza\_Banana](https://discourse.codecombat.com/u/Pizza_Banana)\
**Post date:** [December 3, 2015, 6:39am UTC](https://discourse.codecombat.com/t/noble-sacrifice-help-js/5860/4 "2015-12-03T06:39:47Z")

</div>

Now, there isn’t an error now, but my hero doesn’t collect enough gold.  
Here is my code:

// Collect 80 gold  
var coin = this.findNearest(this.findItems());  
if (this.gold \<= 4 \* this.costOf(“soldier”)) {  
this.moveXY(coin.pos.x, coin.pos.y);  
}  
// Build 4 soldiers to use as bait  
if (this.gold \> 4\*this.costOf(“soldier”)) {  
this.summon(“soldier”);  
this.summon(“soldier”);  
this.summon(“soldier”);  
this.summon(“soldier”);  
}  
// Send your soldiers into position  
var points = [];  
points[0] = { x: 13, y: 73 };  
points[1] = { x: 51, y: 73 };  
points[2] = { x: 51, y: 53 };  
points[3] = { x: 90, y: 52 };  
var friends = this.findFriends();  
var fIndex = 0;  
var friend = friends[fIndex];  
var point = points[i];  
// Use a for-loop to loop over i from 0 to 4  
// Match the friends to the points and command them to move  
if (friend) {  
for (var i = 3; i \> -1; i–) {  
this.command(friend, “move”, point);  
fIndex ++;  
}  
}

---

<div class="post-metadata">

**Author:** ![Hinkle](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hinkle/32/3967_2.png) [@Hinkle](https://discourse.codecombat.com/u/Hinkle)\
**Post date:** [December 3, 2015, 7:02am UTC](https://discourse.codecombat.com/t/noble-sacrifice-help-js/5860/5 "2015-12-03T07:02:23Z")

</div>

Is your coin collection in a loop?  
My Javascript is rusty but (I hope) it should be:

```python

while (this.gold <= 4 * this.costOf("soldier")) {
    var coin = this.findNearest(this.findItems());
    this.moveXY(coin.pos.x, coin.pos.y);
}

```

A while loop keeps on doing something as long as the condition is true, then stops when it is false. Before it was an if statement and would only happen once.

---

<div class="post-metadata">

**Author:** ![Pizza\_Banana](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Pizza\_Banana](https://discourse.codecombat.com/u/Pizza_Banana)\
**Post date:** [December 3, 2015, 4:53pm UTC](https://discourse.codecombat.com/t/noble-sacrifice-help-js/5860/6 "2015-12-03T16:53:23Z")

</div>

this is my code now. It says that the code exceeded the hard execution limit of 3,000,000  
can you tell me the problem?  
’’’  
// Collect 80 gold  
var money = this.gold;  
while (money \<= 80) {  
if (coin) {  
var coin = this.findNearest(this.findItems());  
this.moveXY (coin.pos.x, coin.pos.y);  
}  
}  
// Build 4 soldiers to use as bait  
if (this.gold \> 4 \* this.costOf(“soldier”)) {  
this.summon(“soldier”);  
this.summon(“soldier”);  
this.summon(“soldier”);  
this.summon(“soldier”);  
}  
// Send your soldiers into position  
var points = [];  
points[0] = { x: 13, y: 73 };  
points[1] = { x: 51, y: 73 };  
points[2] = { x: 51, y: 53 };  
points[3] = { x: 90, y: 52 };  
var friends = this.findFriends();  
var fIndex = 0;  
var friend = friends[fIndex];  
var point = points[i];  
// Use a for-loop to loop over i from 0 to 4  
// Match the friends to the points and command them to move  
if (friend) {  
for (var i = 3; i \> -1; i–) {  
this.command(friend, “move”, point);  
fIndex ++;  
}  
}  
’’’

---

<div class="post-metadata">

**Author:** ![Marrrk](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/marrrk/32/3361_2.png) [@Marrrk](https://discourse.codecombat.com/u/Marrrk)\
**Post date:** [December 3, 2015, 7:02pm UTC](https://discourse.codecombat.com/t/noble-sacrifice-help-js/5860/7 "2015-12-03T19:02:44Z")

</div>

There are several problems with your code.

First you grab gold until you reach 80 or more, thats ok so far, but you search only for a coin when you already found a coin. Which is not right as you now might see. Move the coin find line above the condition.

Then you buy 4 soldiers only when and only when you have MORE than 80 gold (four times twenty). In the first part of your code you grab an amount of at LEAST 80 coins, which may under certain circumstances be exactly 80, which will not satisfy your more than 80 gold condition.

In your command part of your code you select a move point by writing point = points[i] but the i variable will only be set after that code, in your for loop.

I can only assume it, but you want to give each soldier an unique point. So your code in this part would be much easier if you iterate from 0 to 3 (or from 3 to 0), then selecting the point by i, then the friend by i and then commanding this friend to this point.

Greetings

---

<div class="post-metadata">

**Author:** ![Hinkle](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hinkle/32/3967_2.png) [@Hinkle](https://discourse.codecombat.com/u/Hinkle)\
**Post date:** [December 4, 2015, 12:21am UTC](https://discourse.codecombat.com/t/noble-sacrifice-help-js/5860/8 "2015-12-04T00:21:31Z")

</div>

Sorry, I forgot that while loops reset faster than loop.

Loop activates once per frame while will go as fast as it can, so if there is no coin it executes over 3,000,000. I made the same mistake myself once before.

You should use loop or put say “There is no coin” if there isn’t a coin.

---

<div class="post-metadata">

**Author:** ![Pizza\_Banana](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Pizza\_Banana](https://discourse.codecombat.com/u/Pizza_Banana)\
**Post date:** [December 4, 2015, 12:35am UTC](https://discourse.codecombat.com/t/noble-sacrifice-help-js/5860/9 "2015-12-04T00:35:38Z")

</div>

Thanks for the help everybody!! I am working on it.

---

<div class="post-metadata">

**Author:** ![Hinkle](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/hinkle/32/3967_2.png) [@Hinkle](https://discourse.codecombat.com/u/Hinkle)\
**Post date:** [December 4, 2015, 2:04am UTC](https://discourse.codecombat.com/t/noble-sacrifice-help-js/5860/10 "2015-12-04T02:04:24Z")

</div>

Okay, I submitted your code and got it to work in JS…  
Several problems mostly stemming from the fact we are using 2 loops instead of 1 loop (which makes thing more complex)

1. Money isn’t set to self.gold in the while loop, so the loop never ends
2. As marrk said you check for coin before you define what they are
3. As marrk said your friends and points are not in the for loop so you will only command 1 soldier to move right. I just commanded friends[fIndex] to move to points[fIndex] inside your loop, but his solution is the one Code Combat encourages.
