Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Diamond Dozen (JavaScript)

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) {
        
    }
    return bestItem;
}

It’s sort of sad, but I got stuck here. What do I do? I know I want to find the highest item valueOverDistance, but how do I do see which item has that?

1 Like

It’s fine to ask for help :smile:

Use the code for finding the enemy with the most health as a template. So you want lines for:

  • Setting a variable item, that’s defined in terms of items and itemIndex
  • Checking if the item valueOverDistance is greater than the current bestValue
  • If it is, setting the bestItem and bestValue variables
  • Increasing the itemIndex

Try it, see how you get on.

2 Likes

Find the best item using valueOverDistance by comparing valueOverDistance with bestValue. If valueOverDistance is larger than bestValue then oh this is exactly what @jka2706 said. Sorry I didn’t read her post properly.

1 Like