I can't beat rich forager in java script

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);
    
    
}

}

please help i cant do it

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?