function findStrongestEnemy() {
var enemies = hero.findEnemies();
var highestHealth = null;
var health = 0;
for (var i = 0; i < enemies.length; i++) {
var enemy = enemies[i];
if (enemy.health >= health) {
highestHealth = enemy;
health = enemy.health;
return highestHealth;
}
}
}
while (true) {
var enemies = hero.findEnemies();
var friends = hero.findFriends();
var highestHealth = null;
var health = 0;
for (var i = 0; i < enemies.length; i++) {
var enemy = enemies[i];
}
if (enemy.health >= health) {
highestHealth = enemy;
health = enemy.health;
for (var i = 0; i < friends.length; i++) {
var friend = friends[i];
if (highestHealth) {
hero.command(friend, "attack", highestHealth);
}
}
}
findStrongestEnemy;
for (var i = 0; i < friends.length; i++) {
findStrongestEnemy;
}
}
Hello guys, this is a friendly reminder that this topic is for assisting @monsterjam23 with code, not talking about why some members put an emoji at the end of their sentences. @SuperSmacker, please bring these kind of discussions in PMs. Thank you.
I solved using 1 while loop and including 2 for loops inside the while statement and 1 if statement. The first for loop iterates through the enemies list to find “if” target is the toughest and the second for loop iterates through the friends list and then hero.command friends to attack toughest.
The process works because it is all inside the while statement.
executes the first for loop.
executes the second for loop.
repeats as long as true;
Below is one format with some information on how you could structure a solution but you fill in the blanks.
while (true) {
var friends = hero.findFriends();
var enemies = hero.findEnemies();
var highestHealth = 0;
var toughest = null;
for (var i = 0; i < ???; i += 1) {
var enemy = ???; // Remember how you would iterate through each enemy from an array
if (??? > highestHealth) { // Think about what enemy we are comparing to find highest health
toughest = enemy;
highestHealth = enemy.health;
}
}
for (var i = 0; i < friends.length; i += 1) {
var friend = friends[i];
hero.command(friend, “attack”, ???); // Think about which enemy we found
}
}
P.S. if you are more comfortable using i++ instead of i += 1 they both work effectively, however take note that the i++ will only increase/increment by 1 while i += (any positive number) has more flexibility.
The principle is the same for the (-) as well just swap the plus signs for decreasing.
At the bottom of your page you have the things that look like brackets. I THINK that you should delete those and you could delete"var health = 0;
for (var i = 0; i < enemies.length; i++) {