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… :frowning:

plz post your code :grinning:

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 :frowning:

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

A better version of that would be:
Javascript:

for (let enemy of hero.findEnemies()) if (enemy.type === "shaman") while (enemy.health > 0) hero.attack(enemy);

Python:

for enemy in hero.findEnemies:
    if (enemy.type === "shaman"):
        while (enemy.health > 0):
            hero.attack(enemy)

Try using for loops for loops like while (NaN < NaN) example:
instead of

while (i < 4) {
    let item = hero.findItems()[i];
    hero.move(item.pos)
}

use

for (let item of hero.findItems()) {
    hero.move(item.pos)
}

and, your main error is that you set the “bestValue” to the ‘item’ and not ‘valueOverDistance(item)’ while setting “bestItem” to what “bestValue” is supposed to be.

Welcome to the forum! This is a family-friendly place where coders can share bugs, ask for help on any CodeCombat level (don’t forget to post your code correctly), or just hang out with other coders. But before you proceed, please check out our guidelines: this topic.
Have a great time! :partying_face:

We are not allowed to give out solutions, only clues. This way the person can think it out themselves and not cheat. Please delete that code and put in its place a few hints and corrections. Thanks, and enjoy your time here!

Thanks for telling me, I’ll edit it. :slight_smile:

1 Like

No problem! Thanks for editing it out!