Spinach power help - Javascript (SOLVED)

Hello, could someone give me a hand please with my code? What happens is the following, the code works, I attack and kill the first enemy but the second enemy does not attack it, what is wrong with my code? Could someone help me please … I’m stuck on this … :frowning:

image

javascript code:

// Collect exactly 7 spinach potions.
// Then you'll be strong enough to defeat the ogres.
var potionCount = 0;

// Wrap the potion collection code inside a while loop.
// Use a condition to check the potionCount
while(potionCount < 7) {
var item = hero.findNearestItem();
    if (item) {
        hero.moveXY(item.pos.x, item.pos.y);
        potionCount++;
    }
}
// When the while loop is finished,
// Go and fight!
var enemy = hero.findNearestEnemy();

while(enemy) {
    if ( hero.isReady("stomp") ) {
        hero.stomp();
    }
    else if (enemy) {
        hero.attack(enemy);
        if (enemy) {
            hero.attack(enemy);
        }
    }
}

You are finding a single enemy, when running your while loop so that it only runs while that enemy exists.
Try Changing your while loop to ‘while True’ and search for the nearest enemy inside of the loop.
Also, you don’t need a second if/attack.

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.