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.

Hi @txt_art Please do not revive this dead topic. @Johnny_Fuentes-McClu may no longer be active and he/she might not see your post. Please do not post solutions as it is against the Discourse rules. @Deadpool198 @Chaboi_3000
Lydia

1 Like

Yes, @txt_art can you delete the working code?

As @Lydia_Song said, posting solutions is not allowed. It defeats the point of the forum. This isn’t a database of solutions, it’s a forum to help people understand their mistakes and fix their code, and then become better coders.
Thanks for trying to help other people, it’s just not quite how we do it :smile:,
Danny

1 Like