# Help on Antipodes [ SOLVED]

So I am unsure as to why there is a code error on the code that it has given me. But when I sent my friend the code it worked on his but not mine:

``````// The warlock used the "clone" spell and created evil antipodes of our archers.
// But even that evil spell has weakness.
// If your archer touches his antipode, then it will disappear.
// If an archer touches the wrong clone or attacks one of them, then the clones start to fight.
// We can find antipodes by their names - they are each other's reverse.
// This function check two units whether they are antipodes or not.
function areAntipodes(unit1, unit2) {
var reversed1 = unit1.id.split("").reverse().join(""); // error here
for(var i =0; i < units.length; i++){
// Iterate all units again with indexes "j".
var iUnits = units[i];
for(var j=0; j<units.length; j++){
// If "i" is equal to "j", then skip (continue).
if (i==j) {
continue;
}
// Find the distance between the i-th and j-th units.
var jUnits = units[j];
var dist = iUnits.distanceTo(jUnits);
// If the distance less than 'minDistance':
if (dist < minDistance) {
// Reassign 'minDistance' with the new distance.
minDistance = dist;
// Reassign 'nearestPair' to the names
// of the current pair of units.
nearestPair = [iUnits.id, jUnits.id];
}
}
}
return reversed1 === unit2.id;
}
var friends = hero.findFriends();
var enemies = hero.findEnemies();
// Find antipodes for each of your archers.
// Iterate all friends.
for (var friend = 0; friend < friends.length; friend++) {
// For each of friends iterate all enemies.
for (var enemy = 0; enemy < enemies.length; enemy++) {
// Check if the pair of the current friend and the enemy are antipodes.
if (areAntipodes(friend, enemy)) {
// If they are antipodes, command the friend move to the enemy.
hero.command(friend, 'move', enemy.pos);
}
}
}
// When all clones disappears, attack the warlock.
var enemies = hero.findEnemies();
while (enemies.length == 1) {
hero.attack(hero.findNearestEnemy());
}

``````

@Johnny_Fuentes-McClu

Sometimes where the ādebugger toolā says there is a problem is not where the code actually needs to be changed.

When looking at your loops that cycle through the enemies and friends, what values are you sending into the function `areAntipodes`()? Are the values numeric, an object, or something else?

You can use `hero.say();` to print the output from a variable to the screen like this:

``````hero.say(friend);
``````
1 Like

I get it now. I keep saying numbers when I need to say names instead right? So to complete this level you need to āconvertā the numbers to names right?

so now when i run it my hero wont attack the warlock

I noticed that your while check is looking for exactly 1 enemy, but the skeletons pop up and make your array more than one which will prevent your hero from attacking the nearest enemy. Iād suggest a check to see if there are no more enemy archers or switch the while to an if check and add the while loop while just for attacking.

``````while (enemies.length == 1)
``````

i donāt think i understand what you mean by āchecking to see if there are no more enemy archersā
and the āadding the while loop while just for attacking.ā parts

I understand the concepts your trying to tell me but not how I would get that done.

Ok, not a problem, Iāll show you what I mean. Are you familiar with the `hero.findByType()`? You can look only for enemy archers using the code below. This will give you an array of all the enemy archers.

``````archers = hero.findByType("archer", hero.findEnemies())
``````

Then see if there are any enemy archers left with:

``````if len(archers) == 0:
``````

The findNearest() method may find the skeletons which makes it difficult to attack the warlock. You may want to focus your attack on the Warlock when the skeletons show up using the same findByType method. Adding the [0] at the end will make the variable only see the first object of the array so it is not an array variable and you can call the attack method on the warlock.

``````warlock = hero.findByType("warlock", hero.findEnemies())[0]
``````

Ok that makes a lot of sense actually. I didnāt know that by adding things in the `hero.findByType()` method you could specify certain allies or enemies. Thank you. knowing that will help me out in the future.

well now my code runs but he moves at the begging and causes me to fail:

Updated code:

``````// The warlock used the "clone" spell and created evil antipodes of our archers.
// But even that evil spell has weakness.
// If your archer touches his antipode, then it will disappear.
// If an archer touches the wrong clone or attacks one of them, then the clones start to fight.
// We can find antipodes by their names - they are each other's reverse.
// This function check two units whether they are antipodes or not.
function areAntipodes(unit1, unit2) {
var reversed1 = unit1.id.split("").reverse().join("");
return reversed1 === unit2.id;
}
var friends = hero.findFriends();
var enemies = hero.findEnemies();
// Find antipodes for each of your archers.
// Iterate all friends.
for (var friend = 0; friend < friends.length; friend++) {
var good = friends[friend];
// For each of friends iterate all enemies.
for (var enemy = 0; enemy < enemies.length; enemy++) {
var bad = enemies[enemy];
// Check if the pair of the current friend and the enemy are antipodes.
var antiPode = areAntipodes(good, bad);
if (antiPode) {
// If they are antipodes, command the friend move to the enemy.
}
}
}
// When all clones disappears, attack the warlock.
var enemyArchers = hero.findByType("archcer", hero.findEnemies());
while (enemyArchers.length === 0) {
var warlock = hero.findByType("warlock", hero.findEnemies())[0];
if (warlock) {
hero.attack(warlock);
}
}

``````

This might just be a spelling error. Your āarcherā, in the findByType is spelled incorrectly.

``````var enemyArchers = hero.findByType("archcer", hero.findEnemies());
``````

but when I change it he still wont attack the warlock.

You have all of the pieces, just need to shuffle them a bit. Add a while (true) loop before all of the code with the enemyArchers and then switch the while (enemyArchers.length) to an if statement.

Since you have to wait for your archers to match up with their antipodes, you need to loop through until they dealt with their double. And then continue the loop to attack the warlock.

If you need more detail - Spoiler for last section
``````while (true){
var enemyArchers = hero.findByType("archer", hero.findEnemies());
if (enemyArchers.length === 0) {
var warlock = hero.findByType("warlock", hero.findEnemies())[0];
if (warlock) {
hero.attack(warlock);
}
}
}

``````

That makes sense. I forgot that if the variables arenāt inside the while loop they will only pop up once and thatās it. Thank you. Ill make sure to keep that in mind for the future.