# Diamond Dozen: The Tricky Dozen

**URL:** <https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376>\
**Category:** Uncategorized\
**Created:** [January 28, 2017, 3:33am UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376 "2017-01-28T03:33:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheShadow](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/theshadow/32/5630_2.png) [@TheShadow](https://discourse.codecombat.com/u/TheShadow)\
**Post date:** [January 28, 2017, 3:33am UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376/1 "2017-01-28T03:33:27Z")

</div>

So I don’t even understand what to do on Diamond Dozen, The commented instructions aren’t very clear and the hints aren’t very useful! My 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 < coins.length) {
    hero.moveXY(items.pos.x, items.pos.y);
    itemsIndex++;
}
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);
    }
}}
```

---

<div class="post-metadata">

**Author:** ![htrnblr](https://avatars.discourse-cdn.com/v4/letter/h/f9ae1b/32.png) [@htrnblr](https://discourse.codecombat.com/u/htrnblr)\
**Post date:** [January 29, 2017, 10:06am UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376/2 "2017-01-29T10:06:13Z")

</div>

You must finish

```python
function findBestItem(items) {
    var bestItem = null;
    var bestValue = 0;
    var itemsIndex = 0;
    
    // Loop over the items array.
    // Find the item with the highest valueOverDistance()
    
    return bestItem;
}

```

using

```python
function valueOverDistance(item) {
    return item.value / hero.distanceTo(item);
}

```

find valueOverDistance() for given item compare to bestValue( Over distance )  
Use as a sample for other parts

```python
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;
}

```

format code propertly

 ![](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/2X/3/3e7233f4fb7b40b4eea7798f19b5b5234c8df4fa.png)

---

<div class="post-metadata">

**Author:** ![TheShadow](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/theshadow/32/5630_2.png) [@TheShadow](https://discourse.codecombat.com/u/TheShadow)\
**Post date:** [February 4, 2017, 4:44pm UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376/3 "2017-02-04T16:44:24Z")

</div>

Hmm… I tried that but it just wont work

---

<div class="post-metadata">

**Author:** ![htrnblr](https://avatars.discourse-cdn.com/v4/letter/h/f9ae1b/32.png) [@htrnblr](https://discourse.codecombat.com/u/htrnblr)\
**Post date:** [February 5, 2017, 6:34am UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376/4 "2017-02-05T06:34:33Z")

</div>

Provide your current code

---

<div class="post-metadata">

**Author:** ![24guppra](https://avatars.discourse-cdn.com/v4/letter/2/c57346/32.png) [@24guppra](https://discourse.codecombat.com/u/24guppra)\
**Post date:** [September 29, 2017, 7:25pm UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376/5 "2017-09-29T19:25:43Z")

</div>

```python
// 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) {

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

```

What is Wrong with my code? I don’t understand what to do?

---

<div class="post-metadata">

**Author:** ![SuperSmacker](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/supersmacker/32/8385_2.png) [@SuperSmacker](https://discourse.codecombat.com/u/SuperSmacker)\
**Post date:** [September 30, 2017, 3:55am UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376/6 "2017-09-30T03:55:56Z")

</div>

> [@24guppra](#):
>
> ```
> // Loop over the items array.
> // Find the item with the highest valueOverDistance()
> while(itemsIndex &lt; items.length) {
> 
> return bestItem;
> }
> 
> ```

You didn’t do that part  
do something like:

```python
for (a in b){
    if (a > greatest){
        greatest = a
    }
}
return a

```

argh i dont do java script  
pls dont chastise me on clean code

---

<div class="post-metadata">

**Author:** ![24guppra](https://avatars.discourse-cdn.com/v4/letter/2/c57346/32.png) [@24guppra](https://discourse.codecombat.com/u/24guppra)\
**Post date:** [October 2, 2017, 7:22pm UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376/7 "2017-10-02T19:22:48Z")

</div>

It still doesn’t work, Tharin won’t move

```python
// 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];
    if (item.value / hero.distanceTo(item) < valueOverDistance(item)){
        bestItem = item;
        bestValue = item.value;
    }
    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);
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![htrnblr](https://avatars.discourse-cdn.com/v4/letter/h/f9ae1b/32.png) [@htrnblr](https://discourse.codecombat.com/u/htrnblr)\
**Post date:** [October 2, 2017, 9:24pm UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376/8 "2017-10-02T21:24:31Z")

</div>

```python
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];
    if (item.value / hero.distanceTo(item) < valueOverDistance(item)){
        bestItem = item;
        bestValue = item.value;
    }
    itemsIndex += 1;
    }
    return bestItem;
}

```

I see some problems here

**First one**

```python
if (item.value / hero.distanceTo(item) < valueOverDistance(item))

```

This line of symbols

```python
valueOverDistance(item)

```

redirect to this function

```python
function valueOverDistance(item) {
    return item.value / hero.distanceTo(item);
}

```

As you can see this this expression

```python
item.value / hero.distanceTo(item) 

```

and function valueOverDistance(item) do the same.

They get Item value for example 3 and divide it on the distance from the hero to an item for example 10 meters  
3g / 10m = 0.3  
This index shows you how much gold you will get per 1 meter of the way  
3g / 15m = 0.2 gold per meter of the way.  
3g / 5m = 0.6 gold per meter of the way.

So the closer item to you and the higher the value of the item the more appealing it is for you.

With numbers your code looks like this:  
If ( 0.3 \< 0.3 )  
{

}

**Second one**

```python
var bestValue = 0;

```

It is really confusing but  
In fact it is bestValue **OverDistance**  
Instead of the current best value over distance for a given item ( use valueOverDistance(item) function for this )  
you assign item.value:

```python
bestValue = item.value;

```

---

<div class="post-metadata">

**Author:** ![24guppra](https://avatars.discourse-cdn.com/v4/letter/2/c57346/32.png) [@24guppra](https://discourse.codecombat.com/u/24guppra)\
**Post date:** [October 6, 2017, 1:41pm UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376/9 "2017-10-06T13:41:06Z")

</div>

I tryed this now. I didn’t work. It will collect coins, but not the right coins.

```python
// 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];
    if (bestValue < valueOverDistance(item)){
        bestItem = item;
        bestValue = item.value;
    }
    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);
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![htrnblr](https://avatars.discourse-cdn.com/v4/letter/h/f9ae1b/32.png) [@htrnblr](https://discourse.codecombat.com/u/htrnblr)\
**Post date:** [October 9, 2017, 2:10pm UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376/10 "2017-10-09T14:10:14Z")

</div>

You did great with the issue number one

BUT

It seems like you completely overlooked the second one

**Second one**

```python
bestValue 

```

It is really confusing but  
In fact it is bestValueOverDistance  
Instead of the current best value over distance for a given item ( use valueOverDistance(item) function for this )  
you assign item.value:

```python
bestValue = item.value;

```

Lets check what is going on in this block

```python
if (bestValue < valueOverDistance(item)){
        bestItem = item;
        bestValue = item.value;
    }

```

again bestValue is the highest valueOverDistance what you encountered up until the current moment  
it is 0 at the start

```python
var bestValue = 0;

```

As the items appear you start to check them  
you check first item - it is gem with value 5 and within 10 meters of you --\> 5 / 10 --\> 0.5  
0 \< 0.5  
so for now  
bestItem becomes that gem  
bestValue becomes 0.5

then you check second item  
It is gold coin - value 3 distance 2 --\> 3 / 2 --\> 1.5  
0.5 \< 1.5  
So for now  
bestItem becomes that gold coin  
bestValue becomes 1.5

and so on until you check all items

---

<div class="post-metadata">

**Author:** ![24guppra](https://avatars.discourse-cdn.com/v4/letter/2/c57346/32.png) [@24guppra](https://discourse.codecombat.com/u/24guppra)\
**Post date:** [October 12, 2017, 7:54pm UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376/11 "2017-10-12T19:54:57Z")

</div>

I tried this, but it still doesn’t work

```python
// 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];
    if (bestValue < valueOverDistance(item)){
        bestItem = item;
        bestValue = valueOverDistance;
    }
    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);
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![htrnblr](https://avatars.discourse-cdn.com/v4/letter/h/f9ae1b/32.png) [@htrnblr](https://discourse.codecombat.com/u/htrnblr)\
**Post date:** [October 12, 2017, 9:37pm UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376/12 "2017-10-12T21:37:13Z")

</div>

> [@24guppra](#):
>
> bestValue = valueOverDistance;

valueOverDistance is a function so () required in this case.  
An item in the parentheses is the thing you pass to function to work with

---

<div class="post-metadata">

**Author:** ![24guppra](https://avatars.discourse-cdn.com/v4/letter/2/c57346/32.png) [@24guppra](https://discourse.codecombat.com/u/24guppra)\
**Post date:** [January 7, 2018, 5:00pm UTC](https://discourse.codecombat.com/t/diamond-dozen-the-tricky-dozen/10376/13 "2018-01-07T17:00:22Z")

</div>

Thank you. My code is now working successfully
