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

Help on Twins Powers

So my code runs it doesn’t show any errors but when he calls the names he doesn’t call the right names and it runs out of time:

// There are four pairs of twins, they should pray by pairs.
// You need to find twins and call them.

// Twins have the same names, only the last letter is different.
// This function checks if the pair of units are twins.
function areTwins(unit1, unit2) {
    var name1 = unit1.id;
    var name2 = unit2.id;
    if (name1.length !== name2.length) {
        return false;
    }
    for (var i = 0; i < name1.length - 1; i++) {
        if (name1[i] !== name2[i]) {
            return false;
        }
    }
    return true;
}

// Iterate over all pairs of paladins and
//  say() their name by pairs if they are twins.
var twins = hero.findFriends();
for(var i = 0; i < twins.length; i++){
    var twin1 = twins[i];
    for (var j = 0; j< twins.length; j++){
        if (i == j){
            continue;
        }
        var twin2 = twins[j];
        hero.say(twin1.id + " " + twin2.id);
    }
}
// For example: hero.say("NameTwin1 NameTwin2")
// When twins are in their spots, lure the ogre.
// Don't be afraid of beams - they are dangerous only for ogres.
var enemies = hero.findEnemies();
var enemy = hero.findNearest(enemies);
var dist = hero.findDistanceTo(enemy);
var returnX = 14;
var returnY = 37;
var returnPoint = (returnX, returnY);
while(true){    
    if (dist < 10) {
        hero.move(returnPoint);
    }
    else {
        hero.move(enemy.pos);
    }
}

Hello,

I don’t know Javascript well, but I don’t see where you call the function to test the twin’s names. I think you will want to add the function before calling the names.

var twin2 = twins[j];
//add function here. 
hero.say(twin1.id + " " + twin2.id);

One extra note: remember that the function returns true or false (if they are twins) so you will want to test the return value before calling the hero’s names.