Help! Diamond Dozen - JS or Any

Hi! I need help, I’m stuck… :frowning:

my code below:

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

The problem isn’t actually on that line, it’s on the ones below. It’s just that an error only occurs when the code runs that line (and not the first time it runs it either).
Look at these two lines:

You are using > to compare value/distance to bestValue. That means bestValue needs to be a number. At the moment you are making it an item. Also, you are defining bestItem as a value, a number: which is valueOverDistance(item). It should be item.
So you need to switch the second halves of both lines around, so the value is being defined as another value, and the item is being defined as another item.
Danny

1 Like

Hmm I think I understood but I am very confused, how should I change this to make it work?

is it good?

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 = value;
            bestItem = 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);
        }
    }
}

This is still wrong. Your right that bestValue should be a value, but you need to define bestValue as the value you compared it to. You compared bestValue to value/distance, not just value. So you need to use value/distance.