[Solved] A little stuck on Twins Power

In this code, the hero runs through every name, but the 1st name always says the same. This is the error message that appears: “TypeError: Cannot read property ‘id’ of undefined.”

var twins = hero.findFriends();
for(var i = 0; i < twins.length; i++) {
  var twin1 = twins[i]; 
 for(var j = 0; twins.length; j++) { 
     if (i == j) {    
     continue;
   }  
  var twin2 = twins[j];
// For example: hero.say("NameTwin1 NameTwin2")
  hero.say(twin1.id + " " + twin2.id);
 }
}

I think the problem is in checking if the 2 elements match, because the error always appears to be coming from twin2.id.

hero.say(twin1.id + " " + twin2.id);

I also think the error could be from the if statement because it might not be checking to see if the twins ids match.

if (i == j) {    
     continue;
   }

is this your whole code?

No, that’s the code that they make you type out.

where is your code? (20

Mod edit: Please do not post successful solutions.

when I run your code, it says infinite loop.

also, when I look at your code, you have never called the function

// 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;
}
//"Where is your areTwins function? You need to call it somewhere."

// 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; twins.length; j++) { 
     if (i == j) {    
     continue;
   }  
  var twin2 = twins[j];
// For example: hero.say("NameTwin1 NameTwin2")
  hero.say(twin1.id + " " + twin2.id);

 }
}

I keep getting the same error when i try to call the function. I’ve also tried to use it like in the level Flawless Pairs.

var twinPair = areTwins();

I know i have to put something in the (), and i know i have to use it somewhere else. I tried it like this:

if (twinPair) {
  hero.say(twin1.id + " " + twin2.id);
}

Do you know how functions work? When you defined the function it requires 2 input, unit1 and unit2. The reason it’s not working is because when you called the function, you didn’t give the inputs that it needs.

Nevermind, I figured it out.

1 Like

Braedon12 you can pass the solutions please?

This level is a (mod edit: no cursing allowed in any language) very big

Sergi_Diaz_Alonso i’m agree with you
braedon 12 pass the full level, i ask you please, if you pass the level i love you for the rest of my life

We do not post solutions here so please don’t ask. If you need assistance with a level then post your code and we will help you figure it out.

Are you agreeing with yourself? Both of these accounts are from the same IP.

2 Likes

Might be the case that they are using a shared computer or using the same IP address. There are several accounts in the forum that has the same IP/

There are some. My daughter has an account and uses the same IP as I do but it’s not likely in this case. Both accounts were created within three minutes of each other.

we are from the same high school and that is why we have the same IP. We need help for this level, we just need to know what is missing

// 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; twins.length; j++) {
if (i == j) {
continue;
}
var twin2 = twins[j];
// For example: hero.say(“NameTwin1 NameTwin2”)
hero.say(twin1.id + " " + twin2.id);

}
}

// When twins are in their spots, lure the ogre.
// Don’t be afraid of beams - they are dangerous only for ogres.
hero.moveXY(63, 38);
hero.moveXY(16, 38);

We need help whit that code, please help us telling us what we need.

Hi, first of all could you post your code formatted, so we can read it and help you. It also keeps indents, like this:

while(true) {
    hero.say("my code is formatted"); 
}

Instructions for doing this are here: https://discourse.codecombat.com/faq
Thanks,
:lion: :lion: :lion: