# Diamond Dozen help - Javascript or any

Hi! I’m doing something wrong, I don’t understand what it is, could someone help me? Please, I would appreciate it very much…

plz post your code

1 Like

like this

``````enemies = hero.findEnemies()
enemyIndex = 0

# Wrap this section in a while loop to iterate all enemies.
# While the enemyIndex is less than the length of enemies
while  enemyIndex < len(enemies):

enemy = enemies[enemyIndex]
if enemy.type == 'shaman':
while enemy.health > 0:
hero.attack(enemy)

# Remember to increment enemyIndex
enemyIndex = enemyIndex + 1

``````
1 Like

Hello, do you think you can help me? I still don’t understand how to solve this

Javascript code:

``````// Claim the coins while defeating the marauding ogres.

function findMostHealth(enemies) {
var target = null;
var targetHealth = 0;
var enemyIndex = 0;
while(enemyIndex < enemies.length) {
var enemy = enemies[enemyIndex];
if(enemy.health > targetHealth) {
target = enemy;
targetHealth = enemy.health;
}
enemyIndex += 1;
}
return target;
}

function valueOverDistance(item) {
return item.value / hero.distanceTo(item);
}

// Return the item with the highest valueOverDistance(item)
function findBestItem(items) {
var bestItem = null;
var bestValue = 0;
var itemsIndex = 0;

// Loop over the items array.
// Find the item with the highest valueOverDistance()
while(itemsIndex < items.length) {
var item = items[itemsIndex];
var value = item.value;
var distance = hero.distanceTo(item);
if (value/distance  > bestValue ) {
bestValue = item;
bestItem = valueOverDistance(item);
}
itemsIndex += 1;
}

return bestItem;
}

while(true) {
var enemies = hero.findEnemies();
var enemy = findMostHealth(enemies);
if(enemy && enemy.health > 15) {
while(enemy.health > 0) {
hero.attack(enemy);
}
} else {
var coins = hero.findItems();
var coin = null;
coin = findBestItem(coins);
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}
}
``````
1 Like