# Misty Island Mine Level Help (JS)

**URL:** <https://discourse.codecombat.com/t/misty-island-mine-level-help-js/29705>\
**Category:** Level Help\
**Created:** [July 19, 2021, 5:06am UTC](https://discourse.codecombat.com/t/misty-island-mine-level-help-js/29705 "2021-07-19T05:06:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FalconX11\_312](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/falconx11_312/32/26330_2.png) [@FalconX11\_312](https://discourse.codecombat.com/u/FalconX11_312)\
**Post date:** [July 19, 2021, 5:06am UTC](https://discourse.codecombat.com/t/misty-island-mine-level-help-js/29705/1 "2021-07-19T05:06:50Z")

</div>

Problem - I don’t know how to check if an item is not in `excludedItems`. I think everything else is fine but here is my code:

```python
function findBestItem(friend, excludedItems) {
    var items = friend.findItems();
    var bestItem = null;
    var bestItemValue = 0;
    for(var i = 0; i < items.length; i++) {
        var item = items[i];
        // indexOf searches and array for a certain element:
        var idx = excludedItems.indexOf(item);
        // If the array doesn't contain it, it returns -1
        // In that case, skip over that item as another peasant is targeting it.
        if(idx != -1) { continue; }
        // Finish the function!
        // Remember bestItemValue should be the highest item.value / distanceTo
        if (!item in excludedItems) {
            if (bestItemValue < item.value / friend.distanceTo(item)) {
                bestItemValue = item.value / friend.distanceTo(item);
                bestItem = item;
            }  
        }
    }
    return bestItem;
}

// This function checks if you have enough gold for a decoy.
function enoughGoldForDecoy() {
    return hero.gold >= 25;
}

while(true) {
    var peasants = hero.findByType("peasant");
    // Create a new array every loop.
    var claimedItems = [];
    for(var i = 0; i < peasants.length; i++) {
        var peasant = peasants[i];
        var enemy = peasant.findNearestEnemy();
        if(enemy) {
            // If the peasant is the target of the enemy
            // AND the hero has enough gold for a decoy
            if (enemy.target == peasant && hero.gold > hero.costOf("decoy")) {
                // Command a peasant to build a "decoy":
                hero.command(peasant, "buildXY", "decoy", peasant.pos.x - 2, peasant.pos.y);
                // Add a continue so the peasant doesn't collect coins when building.
                continue;
            }
            
        }
        var item = findBestItem(peasant, claimedItems);
        if(item) {
            // After an item is claimed, stick it in the claimedItems array.
            claimedItems.push(item);
            // Command the peasant to collect the coin:
            hero.command(peasant, "move", item.pos);
        }
    }
}

```

Help would be most appreciated.

---

<div class="post-metadata">

**Author:** ![FalconX11\_312](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/falconx11_312/32/26330_2.png) [@FalconX11\_312](https://discourse.codecombat.com/u/FalconX11_312)\
**Post date:** [July 19, 2021, 5:32am UTC](https://discourse.codecombat.com/t/misty-island-mine-level-help-js/29705/2 "2021-07-19T05:32:22Z")

</div>

Oh I solved it. (20chars)

---

<div class="post-metadata">

**Author:** ![Matignacio](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/matignacio/32/23165_2.png) [@Matignacio](https://discourse.codecombat.com/u/Matignacio)\
**Post date:** [July 19, 2021, 3:41pm UTC](https://discourse.codecombat.com/t/misty-island-mine-level-help-js/29705/3 "2021-07-19T15:41:50Z")

</div>

Hello, a question, I do not understand, what is the reason why people in the forum put **'20chars’**

---

<div class="post-metadata">

**Author:** ![Lydia\_Song](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/lydia_song/32/31461_2.png) [@Lydia\_Song](https://discourse.codecombat.com/u/Lydia_Song)\
**Post date:** [July 19, 2021, 5:09pm UTC](https://discourse.codecombat.com/t/misty-island-mine-level-help-js/29705/4 "2021-07-19T17:09:19Z")

</div>

Because there is a character limit of 20 on public topics, and 10 on private messages, to prevent spam

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/a/a9445509d1a758c41b5ddcddc8c8e20a5dedb67f.png) [@system](https://discourse.codecombat.com/u/system)\
**Post date:** [July 20, 2021, 5:09am UTC](https://discourse.codecombat.com/t/misty-island-mine-level-help-js/29705/5 "2021-07-20T05:09:52Z")

</div>

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