Diamond Dozen problem


#1

Hi! I have a problem with my code in Diamond Dozen level. I spent some hours on this level and still I can’t figure out my problem. Below I put an image with my code. Please help me!


#2

Problem is with finding highest item.value / hero.distanceTo(item) from my items array, I don’t know what I am doing wrong so if anyone have any idea and can help me I will appreciate it


#3

could you copy your code because it’s really hard to read the pic.


#4

Yeah, sure

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 highest = valueOverDistance(item);
        if (highest > bestValue) {
            bestValue = highest;
            bestItem = item;
        }
        itemsIndex++;
    
    }
    
    return bestItem;
}

The problem is with finding the highest valueOverDistance from my array of items, this should find the highest one and return bestItem, and then collect it, but it doesn’t work.


#5

This is very useful code for replayable treasure levels. You can get more coins more quickly than hero.findNearestItem():wink:


#6

Yeah I like it but still I don’t know what am I doing wrong. Maybe you have any idea?


#7

Hi, can you send us an email at team@codecombat.com and reference this forum post?

I think there may be something wrong with the level itself, but can’t be sure unless I can get your session.


#8

Yeah I’m having the EXACT same problem! I HATE IT!

My code is indentical to yours.