Could someone please help me on Twins Power in Compter Science 5? (JavaScript)
When I run my code, I only get 2 paladins to praise the sun.
I’m finding this level very confusing. @braedon12 Thanks for your help
// 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 paladins = hero.findFriends();
var palaLen = paladins.length;
for (var i = 0; i < palaLen; i++) {
var twin1 = paladins[i];
for (var j = 0; j < palaLen; j++) {
var twin2 = paladins[j];
var check = areTwins(twin1, twin2);
if (check) {
hero.say(twin1 + " " + twin2);
} else if (!check) {
continue;
}
}
}
// 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.