[SOLVED] Diamond Dozen warrior won't go after highest valued coin

I don’t understand why my warrior doesn’t go after the closest high valued coin. I’ve tried many similar topics before starting my own can someone help?

// 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;
}

// Return the item with the highest valueOverDistance(item)
function valueOverDistance(item) {
    return hero.distanceTo(item)/item.value;
}
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) {
        let coin = items[itemsIndex];
        
        if (valueOverDistance(item) > bestValue) {
            bestItem = coin;
            bestValue = bestItem.value;
        }
        itemsIndex += 1;
    }
    return bestItem;
}

while(true) {
    var enemies = hero.findEnemies();
    var enemy = findMostHealth(enemies);
        var coins = hero.findItems();
    if(enemy && enemy.health > 15) {
        while(enemy.health > 0) {
            hero.attack(enemy);
        }
    } else {
        var coin = null;
        coin = findBestItem(coins);
        if(coin) {
            var ready = hero.isReady("jump");
            ready ? hero.jumpTo(coin) : hero.moveXY(coin.pos.x, coin.pos.y);
        }
    }
}

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks! (Yay, I beat @enPointe77 to it :D)
bestValue = bestItem.value; here you need to set it to valueOverDistance()
Also, if it still doesn’t work with that, I will help after my class. :grin:

3 Likes

Thanks moonwatcher348 after your explanation I understand now lol.

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.