# I can't beat rich forager in java script

**URL:** <https://discourse.codecombat.com/t/i-cant-beat-rich-forager-in-java-script/6064>\
**Category:** Uncategorized\
**Created:** [December 12, 2015, 7:18pm UTC](https://discourse.codecombat.com/t/i-cant-beat-rich-forager-in-java-script/6064 "2015-12-12T19:18:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjamin\_McInnis](https://avatars.discourse-cdn.com/v4/letter/b/cab0a1/32.png) [@Benjamin\_McInnis](https://discourse.codecombat.com/u/Benjamin_McInnis)\
**Post date:** [December 12, 2015, 7:18pm UTC](https://discourse.codecombat.com/t/i-cant-beat-rich-forager-in-java-script/6064/1 "2015-12-12T19:18:29Z")

</div>

what am i doing rong  
loop {  
var flag = this.findFlag(flag);  
var enemy = this.findNearestEnemy();  
var item = this.findNearestItem();  
var posItem = item.pos;  
var fx = posItem.x;  
var fy = posItem.y;

```
if (flag) {
    // What happens when I find a flag?
    this.pickUpFlag(flag);
}
else if (enemy) {
    // What happens when I find an enemy?
    this.attack(enemy);
    this.attack(enemy);
}
else if (item) {
    this.moveXY(fx, fy);
    
    
}

```

}

---

<div class="post-metadata">

**Author:** ![Benjamin\_McInnis](https://avatars.discourse-cdn.com/v4/letter/b/cab0a1/32.png) [@Benjamin\_McInnis](https://discourse.codecombat.com/u/Benjamin_McInnis)\
**Post date:** [December 12, 2015, 7:19pm UTC](https://discourse.codecombat.com/t/i-cant-beat-rich-forager-in-java-script/6064/2 "2015-12-12T19:19:13Z")

</div>

please help i cant do it

---

<div class="post-metadata">

**Author:** ![theoden](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/theoden/32/3524_2.png) [@theoden](https://discourse.codecombat.com/u/theoden)\
**Post date:** [December 12, 2015, 9:04pm UTC](https://discourse.codecombat.com/t/i-cant-beat-rich-forager-in-java-script/6064/3 "2015-12-12T21:04:20Z")

</div>

It seems the code is OK.

Are you sure you have the recommended minimum of 400 health points and trying to finish this level with “Submit” and **not** the “Run” button?
