Bookkeeper help JS

my character will not attack the ogres. can someone help me with this

while (true) {
enemies = this.findEnemies;
nearest_enemy = this.findNearest(enemies);
if (enemies && nearest_enemy){
this.attack(nearest_enemy);
}
if (nearest_enemy && nearest_enemy.health <=0){
defeated=defeated+1;
}
if (this.now() > 15){
this.moveXY(59, 33);
this.say( defeated );
break;
}
}
while (true){
items = this.findItems();
nearest_item = this.findNearest(items);
if (nearest_item){
this.move(nearest_item);
}
if (this.now()>30){
this.moveXY(58, 33);
this.say(self.gold);
defeated=0;
break;
}
}
while (true){
enemies = this.findEnemies();
nearest_enemy = this.findNearest(enemies);
if (enemies && nearest){
this.attack(nearest_enemy);
}
if (nearest && nearest.health <=0){
defeated=defeated+1;
}
if (this.now() > 45){
this.moveXY(59, 33);
this.say( defeated );
}
break;
}

The first thing I saw when I looked at your code was that you never introduced your variables with the prefix var, (oh and by the way use that little </> sign in your toolbar to format your code). So to introduce enemies you type var enemies = this.findEnemies();. You never even defined defeated, and when you do you might what to define it outside of a loop so it isn’t reset each time the code runs. Also when you are moving to the coin position you need to qualify your can’t just write this.move(nearest_item);, because nearest_item is an object and you need to move to it’s .pos.

try this!!! it willhelp your code

// Fight enemies for 15 seconds.
// Keep count whenever an enemy is defeated.
var count=0;
while (true) {
var enemy = hero.findNearest(hero.findEnemies());
if (enemy) {hero.attack(enemy); count++;}
if(hero.now() > 15) break;
}
// Tell Naria how many enemies you defeated.
hero.moveXY(58, 33);
hero.say(count);
// Collect coins until the clock reaches 30 seconds.
while (true) {
var item = hero.findNearest(hero.findItems());
hero.moveXY(item.pos.x, item.pos.y);
if(hero.now() > 30) break;
}
// Tell Naria how much gold you collected.
hero.moveXY(58, 33);
hero.say(hero.gold);
// Fight enemies until the clock reaches 45 seconds.
// Remember to reset the count of defeated enemies!
var count1=0;
while (true) {
enemy=hero.findNearest(hero.findEnemies());
if (enemy) {hero.attack(enemy); count1++;}
if(hero.now() > 45) break;
}
// Tell Naria how many enemies you defeated.
hero.moveXY(58, 33);
hero.say(count1);

Hi, please don’t ask for help in two places.
Stick to the other topic. I NEED HELP PLEASE ! js ( bookkeeper )
Danny

ok but i solved level

with that code i was trying to help

the other guy so ya!