Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED]Convenient Enemy Javascript Help

Hello, I do not get what to do to get the last word in this level

// Ogres are hiding in woods. Protect the peasants.
// The last word in the peasants' messages are a hint.

for (var x = 8; x <= 72; x += 16) {
    hero.moveXY(x, 22);
    // Peasants know whom to summon.
    var peasant = hero.findNearest(hero.findFriends());
    var message = peasant.message;
    if (message) {
        // Words are separated by whitespaces.
        var words = message.split(" ");
        // "words" is an array of words from the "message".
        // Get the last word. It's the required unit type.

        // Summon the required unit type.
    }
}


Thanks

word = words[len(words) -1]

I know python, but not javascript. This is how you would do it in python. Then you would summon the variable, in this case summon word.

@sci12 add these after the words= message.split

  var type=words[words.length-1];
        hero.summon(type);

Thank you to @abc and @Falcons118 . Now I know what to do for the first 4! However, My hero does not attack the last ogre

var enemy = hero.findNearestEnemy();
    if (enemy) {
        hero.attack(enemy);
    }

Here is the code I have for the last spot.

put it in a while true loop

var enemy = hero.findNearestEnemy();
while (true) {
    if (enemy) {
        hero.attack(enemy);
    }
}

Hero still does not attack!

What sword do you have?
because my hero one taps the ogre…

Long sword! Though the sword might not be the problem…
Hammers are restricted!

@sci12
try to do this

while(true) {
var enemy= hero.findNearestEnemy();
if (!enemy) {
    hero.moveXY(72, 22);
    enemy = hero.findNearestEnemy();
}
else if (enemy) {
    hero.attack(enemy);
}
}

1 Like

It did the trick! Thank you.

Glad to help :grin:
Mark the post as solved and then we can close this topic

1 Like

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