[SOLVED]Help in aggressive mimicry JS

// Protect the village from the ogres. 
// Watch for ogres, peasants and ogres disguised as peasants.

// This function checks if the text starts with the word.
function startsWith(text, word) {
    // If the word is longer then the text:
    if(word.length > text.length) {
        return false;
    }
    // Loop through the indexes of word and text.
    for (var index = 0; index < word.length; index++) {
        // If characters with the same index are different:
        if (word[index] != text[index]) {
            // Then the word doesn't coincide with the text.
            return false;
        }
    }
    // We checked all letters and they are the same.
    return true;
}

var ogreNameStart = "Zog";

while(true) {
    var suspectFriend = hero.findNearest(hero.findFriends());
    var suspectName = suspectFriend.id;
    // Use the function "startsWith" to check
    // if suspectName starts with "Zog":
    if (startsWith(suspectName, ogreNameStart)) {
        // Then attack suspectFriend:
        hero.attack(suspectFriend);
        var enemy = hero.findNearestEnemy();
    }
    // if there is an enemy, then attack it:
    if (enemy) {
        hero.attack(enemy);
    }
    // Else return to the red X mark:
    else {
        hero.moveXY(27, 27);
    }
}

I think it’s something really obvious but my hero won’t attack ogres in ogre form. @milton.jinich or @Falcons118, plz help

Can I have a link please

Aggressive Mimicry - Learn to Code in Python, JavaScript, HTML | CodeCombat

So do something like this

if startsWith(suspect.id, ogreNameStart):

but in JS

change this

to this

if (startsWith(suspectName,"Zog")) {
    hero.attack(suspectFriend);
    // Then attack suspectFriend:
}

Thanks for translating I don’t know JS :smirk: :smirk:

No, I attack the bad peasants, but I don’t attack the ogres that already look like ogres

then do this

if (enemy) {
        hero.attack(enemy);
    }

more like

elif (enemy) {
        hero.attack(enemy);
    }

never mind, I just figured it out. I didn’t define enemy

Mark who ever post helped you the most in this case it is you since you solved it yourself

Lol he gave himself the solution

Well I did figure it out myself, what?

1 Like

I know nothing wrong I just find it funny

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