[Solved] A little stuck on Twins Power


#1

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;
   }

#2

is this your whole code?


#3

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


#4

where is your code? (20


#5

Here’s the full code if you need it I guess…

// 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);

#6

when I run your code, it says infinite loop.


#7

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);

 }
}

#8

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);
}

#9

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.


#10

Nevermind, I figured it out.